6
votes

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

[email protected] 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

1
please edit your question and provide some code of your ownConstantin Guidon
I edited the post a little bit and added the small code.Dona

1 Answers

0
votes

One issue that can cause this error: check that you're inside of the correct directory.