0

I was following the simple and well-written docs from jest, and I got this test working properly:

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);
});

Now, I wanted to switch to typescript, so I renamed the above file to have .ts extension, invoked yarn add --dev ts-jest (because I prefer to not use Babel) in the command line, and added this import as advised in the docs: import {describe, expect, test} from '@jest/globals';

However, after doing the above, I get this error message when calling 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)

Am I following the guide in a wrong way? Did I miss any step? In the guide, there's no module defined in the code snippet.

UPDATE: I've tried running the commands yarn add --dev ts-node and yarn jest --init, to no avail, yielding this new error message:

% 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.

1 Answer 1

0

When using jest with typescript it's actually better to look at this reference guide instead: https://kulshekhar.github.io/ts-jest/docs/getting-started/installation/

I did follow it and I didn't have any of the problems above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.