简体   繁体   中英

Testing with Jest - TypeError: cannot read property 'forEach' of undefined

I created a sample project to write tests with Jest. I followed some tutorials.

I added a simple calculation function in a typescript file as below.

Calc.cs

 export class Calc {
    public add(num1: number, num2: number) : number {
      return num1 + num2;
    }
    public subtract(num1: number, num2: number) : number {
      return num1 - num2;
    }
  }

Then added a test file as below.

import { should } from 'chai';
import { Calc } from './Calc';
should();


let calc = new Calc();
test('adds 1 + 2 to equal 3', () => {
    expect(calc.add(1, 2)).toBe(3);
  });

Then I tried to run the tests as mentioned below. Then I got this error.

PS C:TestProject> npm t

typescriptseleniumtestproject@1.0.0 test C:TestProject jest

C:TestProject\\node_modules\\jest-haste-map\\build\\crawlers\\node.js:49 names.forEach(file => { ^

TypeError: Cannot read property 'forEach' of undefined at default.readdir (C:\\TestProject\\node_modules\\jest-haste-map\\build\\crawlers\\node.js:49:13) at go$readdir$cb (C:\\TestProject\\node_modules\\jest\\node_modules\\graceful-fs\\graceful-fs.js:149:14) at FSReqWrap.oncomplete (fs.js:135:15) npm ERR! Test failed. See above for more details.

Then I browse to that file which shows an error and it is mentioned below.

node_modules\\jest-haste-map\\build\\crawlers\\node.js

可能导致此错误的一个问题:检查您是否在正确的目录中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM