2
//something.spec.js
import { myCustomGlob } from './scripts/index'
describe.only('generate index', () => {
    console.log(myCustomGlob) //MODULE NOT FONUD ERROR
})

//script
const fs = require('fs')
const myCustomGlob = () => {
    //some fs operation
}
export {
    myCustomGlob
}

I have a npm script that used glob and fs to generate some code to create a new file. But I want to write unit test for those function too, but I got MODULE 'fs' NOT FOUND error. why? I thought 'fs' is included since I import myCustomGlob?

  • You are using type script that's new for me but i can see you are using two types syntax for import module in you page. So kindly check your fs importing syntax may be incorrect. – Devraj verma Jul 09 '18 at 05:27
  • @Devrajverma does it matter? I can run the script using npm run. The problem is my spec file. –  Jul 09 '18 at 05:29
  • @Devrajverma that's not typeScript , @melissa92 try : `import {fs} from 'fs';` instead of `import 'fs' from 'fs'` – Taki Jul 09 '18 at 05:31
  • @melissa92 you also can use const fs = rquire('fs') instead of import 'fs' from 'fs' . – Devraj verma Jul 09 '18 at 05:33
  • Possible duplicate of [Using import fs from 'fs'](https://stackoverflow.com/questions/43622337/using-import-fs-from-fs) – Taki Jul 09 '18 at 05:34
  • @Taki thanks for correct me. But syntax are looking same as type script. – Devraj verma Jul 09 '18 at 05:35
  • @Taki that's not the problem, the problem is at my spec.js –  Jul 09 '18 at 06:13

1 Answers1

0

Try import * as fs from 'fs' or just try removing the quotes from the first fs. You are looking up the module with a string, that doesn't work.

Evelijn
  • 371
  • 2
  • 10
  • it's not about the import in my script, it's error from spec file. –  Jul 09 '18 at 06:13
  • Well the error clearly has something to do with the spec script not being able to find the module it needs. I wrote this before the person asking the question changed her code, it was still relevant then. Although I think it's still relevant because the error says it can't find the module it needs. – Evelijn Jul 09 '18 at 06:24
  • @Evelijn exactly –  Jul 09 '18 at 06:25
  • @Melissa92 So have you tried a actually importing fs module in the spec? The spec needs it too in order to know what fs is. – Evelijn Jul 09 '18 at 06:35
  • @Evelijn tried that of course, same error. Not sure it has to do with my karma config, so far I haven't have that kind of error at all. –  Jul 09 '18 at 06:37
  • Have you also defined fs in the karma config? – Evelijn Jul 09 '18 at 06:45
  • @Melissa92 sorry I forgot to mention you. So fs as a dependency in the karma config, have you tried that? – Evelijn Jul 09 '18 at 06:56
  • I found the issue, it was because I ignore node_module, because I use to test mostly frontend code which doesn't require node_module at all, but when you have something that use fs, path, and glob, you would want to use that, not sure if I should customize the karma config to use node_module for this single test only. –  Jul 09 '18 at 06:58
  • @Melissa92 Ah that makes sense, nice catch! I think the matter of customizing the karma config is something you should decide for yourself. If this is indeed the only test you need node_modules for, chances are such a exception is sufficient. – Evelijn Jul 09 '18 at 07:09
  • @Evelijn I'm testing npm script which generate a bunch of code, does it make sense to even test the function for it? it doesn't seem the logic will fail, and I don't think will add more codes to the script in the future, it's strange to test that isn't it? most likely it will never fail. –  Jul 09 '18 at 07:29