reactjs - Use scoped packages with Jest -
i developing app react-native , typescript , doing tests jest, have problem when use scoped packages (@assets), jest can not find path , gives error.
the directory structure looks this:
project/ assets/ img/ foo.png package.json src/ foo.ts build/ foo.js // assets/package.json { "name": "@assets" // @assets scope } // build/foo.js const image = require('@assets/img/foo.png'); // <- error in jest
so when run jest:
npm run jest build/
it can not find '@assets/img/foo.png' , throws error:
cannot find module '@assets/img/logo.png' 'foo.js'
how can use scope package in jest?
jest version: 20.0.4
thanks
necessary define modulenamemapper
in jest config:
// package.json "jest": { "modulenamemapper": { "^@assets.+\\.(png)$": "<rootdir>/assetstransformer.js" }, } // assetstransformers.js const path = require('path'); module.exports = { process(src, filename, config, options) { return 'module.exports = ' + json.stringify(path.basename(filename)) + ';'; }, };
thanks this comment :-)
wiki
Comments
Post a Comment