As I delved into the clear and concise jest documentation, I managed to successfully implement this test:
const { spawnSync } = require('child_process');
const ls = spawnSync('ls', ['-lh', '/usr']);
const unexistent = spawnSync('thiscommandshouldnotexist', ['-lh', '/']);
test('spawnSync1', () => {
expect(ls.error).toBe(undefined);
});
test('spawnSync2', () => {
expect(unexistent.error).not.toBe(undefined);
});
But now, my objective was to transition to typescript. So, I changed the file extension to .ts
, executed yarn add --dev ts-jest
(opting not to use Babel), and included the recommended import from the docs:
import {describe, expect, test} from '@jest/globals';
Despite following these steps, I encountered an error message after running yarn jest
:
/Users/knocte/Documents/Code/myrepo/somefilename.test.ts:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){import { describe, expect, test } from '@jest/globals'; SyntaxError: Cannot use import statement outside a module at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1678:14)
Have I overlooked a step in the guide? Is there a mistake in how I am proceeding? The code snippet in the guide does not reference any module.
UPDATE: In attempting the commands yarn add --dev ts-node
and yarn jest --init
, I faced a new error:
% yarn jest
yarn run v1.22.19
$ /Users/knocte/Documents/Code/myrepo/node_modules/.bin/jest
Error: Jest: Failed to parse the TypeScript config file /Users/knocte/Documents/Code/myrepo/jest.config.ts
Error: Cannot find module 'typescript'
Require stack:
- /Users/knocte/Documents/Code/myrepo/node_modules/ts-node/dist/util.js
- /Users/knocte/Documents/Code/myrepo/node_modules/ts-node/dist/index.js
at readConfigFileAndSetRootDir (/Users/knocte/Documents/Code/myrepo/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:136:13)
at async readConfig (/Users/knocte/Documents/Code/myrepo/node_modules/jest-config/build/index.js:216:18)
at async readConfigs (/Users/knocte/Documents/Code/myrepo/node_modules/jest-config/build/index.js:404:26)
at async runCLI (/Users/knocte/Documents/Code/myrepo/node_modules/@jest/core/build/cli/index.js:182:59)
at async Object.run (/Users/knocte/Documents/Code/myrepo/node_modules/jest-cli/build/cli/index.js:155:37)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.