Automation Course "Setting up Our Frameworks With Our Libraries " error when running test

I am following the tutorial 2.2.3 "Setting up Our Frameworks With Our Libraries
".
When I run

node_modules/jest/bin/jest.js src/__tests__/exampleComponent.test.js

the jest.js file just opens and the test does not run?


Steps taken:
Forked repository
cloned forked repository
npm install -save @testing-library/react jest nock jest-junit
created tests folder in src folder
created exampleComponent.test.js in tests folder
added test('This is an example component test', () => { console.log("I was run"); }); to test file
ran node_modules/jest/bin/jest.js src/__tests__/exampleComponent.test.js

1 Like

Solved, issue seemed to be the Jest CLI script was run in an ES module environment and not CommonJS environment so the β€˜__filename’ variable was not available, after some chatgpting I solved by

1. Create a Custom Jest Script

Create a simple JavaScript file to require and run Jest. This script should be compatible with both CommonJS and ES module environments.

Create a Script File:

Create a file named runJest.js in the root of your project:


// runJest.js
const jest = require('jest');

const argv = process.argv.slice(2);
jest.run(argv);

2.Update package.json Scripts:

Add a new script in your package.json that uses this custom file to run Jest:

"scripts": {
  "testj": "node runJest.js"
}

3.Run the Tests:

Now, you can run the custom script using npm:


npm run testj
1 Like