@teambit/jest
Version:
88 lines (86 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.JestWorker = void 0;
function _flatted() {
const data = require("flatted");
_flatted = function () {
return data;
};
return data;
}
function _worker() {
const data = require("@teambit/worker");
_worker = function () {
return data;
};
return data;
}
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class JestWorker {
constructor() {
_defineProperty(this, "onTestCompleteCb", void 0);
}
onTestComplete(onTestComplete) {
this.onTestCompleteCb = onTestComplete;
// return this;
}
watch(jestConfigPath, testFiles, rootPath, jestModulePath, envRootDir) {
return new Promise(resolve => {
// TODO: remove this after jest publish new version to npm: https://github.com/facebook/jest/pull/10804
// eslint-disable-next-line
console.warn = function () {};
/* The path to the jest config file. */
// eslint-disable-next-line import/no-dynamic-require,global-require
const jestConfig = require(jestConfigPath);
// eslint-disable-next-line import/no-dynamic-require,global-require
const jestModule = require(jestModulePath);
const jestConfigWithSpecs = Object.assign(jestConfig, {
testMatch: testFiles
});
const config = {
// Setting the rootDir to the env root dir to make sure we can resolve all the jest presets/plugins
// from the env context
rootDir: envRootDir,
// Setting the roots (where to search for spec files) to the root path (either workspace or capsule root)
// TODO: consider change this to be an array of the components running dir.
// TODO: aka: in the workspace it will be something like <ws>/node_modules/<comp-package-name>/node_modules/<comp-package-name>
// TODO: see dependencyResolver.getRuntimeModulePath (this will make sure the peer deps resolved correctly)
// TODO: (@GiladShoham - when trying to set it to this paths, jest ignores it probably because the paths contains "node_modules"
// TODO: trying to set the https://jestjs.io/docs/27.x/configuration#testpathignorepatterns-arraystring to something else (as it contain node_modules by default)
// TODO: didn't help)
roots: [rootPath],
// useStderr: true,
// TODO: check way to enable it
runInBand: true,
silent: false,
watch: true,
watchAll: true,
watchPlugins: [[`${__dirname}/watch.js`, {
specFiles: testFiles,
onComplete: results => {
if (!this.onTestCompleteCb) return;
try {
const json = (0, _flatted().parse)((0, _flatted().stringify)(results));
this.onTestCompleteCb(json);
// disable eslint because we want to catch error but not print it on worker
// eslint-disable-next-line
} catch (error) {}
}
}]]
};
const withEnv = Object.assign(jestConfigWithSpecs, config);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
const res = jestModule.runCLI(withEnv, [jestConfigPath]);
// eslint-disable-next-line no-console
res.catch(err => console.error(err));
resolve();
});
}
}
exports.JestWorker = JestWorker;
(0, _worker().expose)(new JestWorker());
//# sourceMappingURL=jest.worker.js.map