Cant import regular js files when using create-react-app-typescript #120
Description
I have some .js + .d.ts files which i am using in my create-react-app-typescript project. Everything works and runs fine with npm run start or npm run build.
When running npm run test, the import statements for these .js files return the name of the file, instead of actually importing the file.
I believe this behaviour is caused by the configuration line in "createJestConfig.js". The matches all .js files and replaces them with stubs:
'^(?!.*\\.(css|json)$)': resolve('config/jest/fileTransform.js'),
When adding .js to the ignored transform patterns, the tests correctly import my .js+.d.ts files
transformIgnorePatterns: [
'[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$',
'^.+\\.js$' // I ADDED THIS LINE
],
I believe this is a bug/oversight in the configuration, since using .js+.dts files as source code is not that strange. In any case i do not think it makes sense to replace all .js files with stubs.
I would love to see this fixed in the project or a way to configure this without the massive workaround i have posted below
Thanks,
MH
PS
I use this workaround to prevent changing the react-scripts ts:
var Module = require('module');
Module.prototype.require = new Proxy(Module.prototype.require, {
apply(target, thisArg, argumentsList){
let name = argumentsList[0];
if (name === "./utils/createJestConfig") {///\.\/utils\/createJestConfig/g.test(name)){
var ret = Reflect.apply(target, thisArg, argumentsList);
var ori = ret;
createJestConfig = function (a, b, c) {
var ret = ori(a, b, c);
ret.transformIgnorePatterns.push('^.+\\.js$');
return ret;
}
return createJestConfig;
}
else
return Reflect.apply(target, thisArg, argumentsList)
}
})
require("react-scripts-ts/scripts/test");