0

I have a relatively simple Node.js application in which I'm trying to use Jest for my unit tests (since I use it on my other client project and would like the consistency of using it here). To try to get to the bottom of it, I have tried to create a stripped-down project with the bare minimum to reproduce the problem.

My package.json is here:

{
  "name": "jest-sample",
  "version": "0.2.0",
  "private": true,
  "description": "NodeJS backend to the Service Manager UI",
  "author": "Me",
  "license": "SEE LICENSE IN LICENSE",
  "deprecated": false,
  "main": "src/js/app.js",
  "type": "module",
  "engines": {
    "node": "^8.15.1"
  },
  "scripts": {
    "test": "npx jest"
  },
  "devDependencies": {
    "jest": "26.0.1",
    "supertest": "4.0.2"
  },
  "jest": {
    "testEnvironment": "node"
  }
}

I don't have eslint or anything else in the project.

➜  jest-sample ls -a
.                 ..                __tests__         node_modules      package-lock.json package.json

The test is also simple:

describe('Sample Test', function() {
    it('should be', function() {
        expect(true).toBe(true);
    })
});

Yet, when I run jest, I get:

➜  jest-sample npx jest
 FAIL  __tests__/sample.spec.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', function() {
        | ^
      2 |     it('should be', function() {
      3 |         expect(true).toBe(true);
      4 |     })

      at Object.<anonymous> (__tests__/sample.spec.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.562 s
Ran all test suites.

UPDATE: The output of jest-debug is below:

➜  jest-sample npx jest --debug
{
  "configs": [
    {
      "automock": false,
      "cache": true,
      "cacheDirectory": "/private/var/folders/4m/6tytrddj3g90b8rmbrccw3lm0000gn/T/jest_dx",
      "clearMocks": false,
      "coveragePathIgnorePatterns": [
        "/node_modules/"
      ],
      "cwd": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
      "detectLeaks": false,
      "detectOpenHandles": false,
      "errorOnDeprecated": false,
      "extraGlobals": [],
      "forceCoverageMatch": [],
      "globals": {},
      "haste": {
        "computeSha1": false,
        "throwOnModuleCollision": false
      },
      "moduleDirectories": [
        "node_modules"
      ],
      "moduleFileExtensions": [
        "js",
        "json",
        "jsx",
        "ts",
        "tsx",
        "node"
      ],
      "moduleNameMapper": [],
      "modulePathIgnorePatterns": [],
      "name": "d652708218018aa212b40c53d7999ccc",
      "prettierPath": "prettier",
      "resetMocks": false,
      "resetModules": false,
      "restoreMocks": false,
      "rootDir": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
      "roots": [
        "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample"
      ],
      "runner": "jest-runner",
      "setupFiles": [],
      "setupFilesAfterEnv": [],
      "skipFilter": false,
      "snapshotSerializers": [],
      "testEnvironment": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/jest-environment-node/build/index.js",
      "testEnvironmentOptions": {},
      "testLocationInResults": false,
      "testMatch": [
        "**/__tests__/**/*.[jt]s?(x)",
        "**/?(*.)+(spec|test).[tj]s?(x)"
      ],
      "testPathIgnorePatterns": [
        "/node_modules/"
      ],
      "testRegex": [],
      "testRunner": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/jest-jasmine2/build/index.js",
      "testURL": "http://localhost",
      "timers": "real",
      "transform": [
        [
          "^.+\\.[jt]sx?$",
          "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/babel-jest/build/index.js",
          {}
        ]
      ],
      "transformIgnorePatterns": [
        "/node_modules/"
      ],
      "watchPathIgnorePatterns": []
    }
  ],
  "globalConfig": {
    "bail": 0,
    "changedFilesWithAncestor": false,
    "collectCoverage": false,
    "collectCoverageFrom": [],
    "coverageDirectory": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/coverage",
    "coverageProvider": "babel",
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "detectLeaks": false,
    "detectOpenHandles": false,
    "errorOnDeprecated": false,
    "expand": false,
    "findRelatedTests": false,
    "forceExit": false,
    "json": false,
    "lastCommit": false,
    "listTests": false,
    "logHeapUsage": false,
    "maxConcurrency": 5,
    "maxWorkers": 3,
    "noStackTrace": false,
    "nonFlagArgs": [],
    "notify": false,
    "notifyMode": "failure-change",
    "onlyChanged": false,
    "onlyFailures": false,
    "passWithNoTests": false,
    "projects": [],
    "rootDir": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample",
    "runTestsByPath": false,
    "skipFilter": false,
    "testFailureExitCode": 1,
    "testPathPattern": "",
    "testSequencer": "/Users/rbair/Projects/gitlab/iot/infrastructure/service-manager/jest-sample/node_modules/@jest/test-sequencer/build/index.js",
    "updateSnapshot": "new",
    "useStderr": false,
    "watch": false,
    "watchAll": false,
    "watchman": true
  },
  "version": "26.0.1"
}
 FAIL  __tests__/sample.spec.js
  ● Test suite failed to run

    ReferenceError: describe is not defined

    > 1 | describe('Sample Test', function() {
        | ^
      2 |     it('should be', function() {
      3 |         expect(true).toBe(true);
      4 |     })

      at Object.<anonymous> (__tests__/sample.spec.js:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.523 s
Ran all test suites.
12
  • Why are you using npx jest? Also that passes for me with your package.json, please give a minimal reproducible example. Commented Jun 4, 2020 at 19:13
  • I want to use the jest that is installed by npm install, and not require a global install. I'm new to Jest and fairly new to npm, so maybe there is a better way than npx? If the example works for you, then there must be something else in my environment causing the problem. Any ideas what could cause that? Commented Jun 4, 2020 at 20:21
  • "test": "jest" already uses the jest locally installed in ./node_modules/.bin Commented Jun 4, 2020 at 20:22
  • Cool, thanks for the tip! Commented Jun 4, 2020 at 20:24
  • 1
    Interestingly I do see that behaviour in Node 10.13.0, 10.14.0, 10.14.1; seems to be fixed from 10.14.2 (release notes: github.com/nodejs/node/blob/master/doc/changelogs/…). I'd recommend upgrading Node if you can. Commented Jun 4, 2020 at 20:47

1 Answer 1

1

As discovered by @jonrsharpe, my underlying problem was the version of Node I was using (10.13.0). By updating to version 12.18.0, the issue went away.

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.