IDEA - Javascript Test with Jest

> Integrated development environment (ide) > IntelliJ - IDEA

1 - About

Javascript Test with Jest in IDEA

Advertising

3 - Structure

  • Source File: auth.js
  • Test File: auth.test.js. Go to test file from source file: Ctrl+Shift+T or Navigate > Test

The test file:

  • name should have a .test., .spec. or _spec. suffix
  • location should be either next to the source file or in a test folder.

4 - Steps

4.1 - Install the node plugin

Jest is a node process, the node plugin must be installed. See node plugin

4.2 - Javascript Language

Jest use es6 language:

4.3 - Install jest

4.4 - Run configuration

To get run configuration automatically and as gutter, you can add the rc-producer.yml run configuration file.

Add the file rc-producer.yml to the subdirectory ./idea - rc means run configuration. See this file for the meaning of each property.

rc-producer.yml
- &defaults
  files: ["**/__tests__/**/*", "**/*.spec.*", "**/*.test.*", "!**/node_modules/**/*"]
  
  script: "node_modules/jest/bin/jest.js"
  scriptArgs: ["-i", &filePattern '--testPathPattern=[/\\]{1}${fileNameWithoutExt}\.\w+$']
  rcName: "${fileNameWithoutExt}"
 
-
  <<: *defaults
  lineRegExp: '^\s*(?:test|it|describe)(?:\.[^(]+)?\((?:"|''){1}([^"'']+)'
  scriptArgs: ["-i", "-t", "${0regExp}", *filePattern]
  rcName: "${fileNameWithoutExt}.${0}"
Advertising

4.5 - Add jest as global library

Jest doesn't use any import statement and therefore the syntax checking may report an Missing import statement. To avoid it, you need to install the Jest library as a global library.

  • File Settings (Ctrl+Alt+S)
  • And…

Then, the errors will disappear but you get also a little bit of completion.

4.6 - Create a test in the test directory:

base.test.js
test('adds 1 + 2 to equal 3', () => {
    expect(1 + 2).toBe(3);
});

4.7 - Configure it

By default, jest is looking in all directory to search for test files. This takes a lot of time. You can restrain this behavior by adding properties in the package.json file

package.json
{
  ...............
  "jest": {
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "roots": ["<rootDir>/test/"]
  },
  ...............
}

4.8 - Run it

  • You can then run a test from everywhere.
    • Click the left gutter. Choose Run <test_name> or Debug <test_name> from the pop-up list.
    • Run/Debug Configuration
    • Ctrl + Enter when on the test function name
    • Right+Click on the file + Run

C:\Users\gerard\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\172.3757.52\bin\runnerw.exe 
D:\nodejs\node.exe D:\myProject\node_modules\jest\bin\jest.js -i --testPathPattern=[/\\]{1}base.test\.\w+$
 PASS  test\base.test.js
  √ adds 1 + 2 to equal 3 (6ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.03s
Ran all test suites matching /[\/\\]{1}base.test\.\w+$/i.
Advertising

5 - Documentation / Reference

ide/idea/javascript_test.txt · Last modified: 2019/05/13 07:58 by gerardnico