Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Jest projects + javascript-algorithms #481

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dbux-babel-plugin/src/visitors/programVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ function enter(buildCfg, path, state) {
filePath,
} = state;

// debug(`babel-plugin: ${filePath}`);

// staticProgramContext
const staticProgramContext = {
type: 1, // {StaticContextType}
Expand Down
5 changes: 4 additions & 1 deletion dbux-cli/src/util/buildBabelOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export default function buildBabelOptions(options) {
verbose = 0
} = options;

// console.log(`buildBabelOptions verbose=${verbose}`);

if (dontInjectDbux && !esnext) {
// nothing to babel
return null;
Expand All @@ -87,8 +89,9 @@ export default function buildBabelOptions(options) {
sourceMaps: 'inline',
ignore: [
// '**/node_modules/**',
function shouldIgnore(modulePath) {
function shouldIgnore(modulePath, ...otherArgs) {
if (!modulePath) {
verbose && debugLog(`[Dbux] no modulePath`, ...otherArgs);
return undefined;
}

Expand Down
18 changes: 18 additions & 0 deletions dbux-projects/assets/javascript-algorithms/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// NOTE: Jest apparently does its own module resolution, making it impossible to use `babel-register`

module.exports = {
"presets": ["@babel/preset-env"],
"plugins": [
["@dbux/babel-plugin", {
verbose: 1
}]],

ignore: [
// '**/node_modules/**',
function shouldIgnore(modulePath, ...otherArgs) {
const allow = true;
console.debug(`[Dbux] BABEL`, modulePath, allow);
return !allow;
}
]
};
5 changes: 5 additions & 0 deletions dbux-projects/assets/javascript-algorithms/dbuxJestSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// NOTE: Jest forcefully exits if asynchronous operations don't finish within 1s.
// We can use this workaround (with `setupFilesAfterEnv`) to make sure, it will wait for Dbux to finish.
afterAll(() => {
return global.__dbux__.client.waitForQueue();
});
55 changes: 40 additions & 15 deletions dbux-projects/src/projectLib/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,19 +368,7 @@ This may be solved by pressing \`clean project folder\` button.`);
* NOTE: This method is called by `gitClone`, only after a new clone has succeeded.
*/
async install() {
// remove files
let { projectPath, rmFiles } = this;
if (rmFiles?.length) {
const absRmFiles = rmFiles.map(fName => path.join(projectPath, fName));
const iErr = absRmFiles.findIndex(f => !f.startsWith(projectPath));
if (iErr >= 0) {
throw new Error('invalid entry in `rmFiles` is not in `projectPath`: ' + rmFiles[iErr]);
}
this.logger.warn('Removing files:', absRmFiles);
sh.rm('-rf', absRmFiles);
}

// copy assets
// remove and copy assets
await this.installAssets();
await this.autoCommit(); // auto-commit -> to be on the safe side

Expand Down Expand Up @@ -529,13 +517,26 @@ This may be solved by pressing \`clean project folder\` button.`);
* Copy all assets into project folder.
*/
async installAssets() {
// remove unwanted files
let { projectPath, rmFiles } = this;
if (rmFiles?.length) {
const absRmFiles = rmFiles.map(fName => path.join(projectPath, fName));
const iErr = absRmFiles.findIndex(f => !f.startsWith(projectPath));
if (iErr >= 0) {
throw new Error('invalid entry in `rmFiles` is not in `projectPath`: ' + rmFiles[iErr]);
}
this.logger.warn('Removing files:', absRmFiles);
sh.rm('-rf', absRmFiles);
}

// copy assets
const folders = this.getAllAssetFolderNames();
folders.forEach(folderName => {
this.copyAssetFolder(folderName);
});

// make sure, we have node at given version and node@lts
if (this.nodeVersion) {
// make sure, we have node at given version and node@lts
await this.exec(`volta fetch node@${this.nodeVersion} node@lts npm@lts`);
await this.exec(`volta pin node@${this.nodeVersion}`);
}
Expand Down Expand Up @@ -569,6 +570,7 @@ This may be solved by pressing \`clean project folder\` button.`);
const assets = fs.readdirSync(this.getAssetDir(folderName));
assets.forEach(assetName => {
files.add(assetName);
this.logger.debug('asset', folderName, assetName);
});
});

Expand Down Expand Up @@ -699,7 +701,15 @@ This may be solved by pressing \`clean project folder\` button.`);
return {
require: bug.require,
keepAlive: bug.keepAlive,
mochaArgs: this.getMochaRunArgs(bug, moreMochaArgs)
testArgs: this.getMochaRunArgs(bug, moreMochaArgs)
};
}

getJestCfg(bug, moreJestArgs) {
return {
require: bug.require,
// keepAlive: bug.keepAlive,
testArgs: this.getJestRunArgs(bug, moreJestArgs)
};
}

Expand All @@ -719,6 +729,21 @@ This may be solved by pressing \`clean project folder\` button.`);
return argArray.join(' '); //.map(s => `"${s}"`).join(' ');
}

/**
* @see https://mochajs.org/#command-line-usage
*/
getJestRunArgs(bug, moreArgs = EmptyArray) {
// bugArgs
const argArray = [
...moreArgs,
...(bug.runArgs || EmptyArray)
];
if (argArray.includes(undefined)) {
throw new Error(bug.debugTag + ' - invalid `Project bug`. Arguments must not include `undefined`: ' + JSON.stringify(argArray));
}
return argArray.join(' '); //.map(s => `"${s}"`).join(' ');
}

// ###########################################################################
// logging
// ###########################################################################
Expand Down
2 changes: 1 addition & 1 deletion dbux-projects/src/projects/eslint/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class EslintProject extends Project {

const mochaCfg = {
cwd: projectPath,
mochaArgs: `${bugArgs} ${files.join(' ')}`,
testArgs: `${bugArgs} ${files.join(' ')}`,
require: [
...(bug.require || EmptyArray),
this.manager.getDbuxPath(`@dbux/runtime/deps/require.ws.${nodeVersion}.js`)
Expand Down
6 changes: 3 additions & 3 deletions dbux-projects/src/projects/express/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export default class ExpressProject extends Project {
'should work with IPv6 Host'
],
testFilePaths: ['test/req.host.js'],
mochaArgs: '--globals setImmediate,clearImmediate',
testArgs: '--globals setImmediate,clearImmediate',
require: [], // has no test.env
bugLocations: [
{
Expand Down Expand Up @@ -344,7 +344,7 @@ export default class ExpressProject extends Project {
id: 25,
testRe: 'should ignore object callback parameter with jsonp',
testFilePaths: ['test/res.jsonp.js'],
mochaArgs: '--globals setImmediate,clearImmediate',
testArgs: '--globals setImmediate,clearImmediate',
require: [],
bugLocations: [241, 242].map(line => ({
fileName: 'lib/response.js',
Expand Down Expand Up @@ -395,7 +395,7 @@ export default class ExpressProject extends Project {
runArgs: [
'--grep',
`"${testRe}"`,
...(bug.mochaArgs ? [bug.mochaArgs] : []),
...(bug.testArgs ? [bug.testArgs] : []),
'--',
...bug.testFilePaths
],
Expand Down
4 changes: 2 additions & 2 deletions dbux-projects/src/projects/hexo/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ export default class HexoProject extends Project {

async testBugCommand(bug, cfg) {
const { projectPath } = this;
const bugArgs = this.getMochaRunArgs(bug);
const testArgs = this.getMochaRunArgs(bug);

const mochaCfg = {
cwd: projectPath,
mochaArgs: bugArgs,
testArgs,
require: bug.require,
...cfg
};
Expand Down
31 changes: 22 additions & 9 deletions dbux-projects/src/projects/javascript-algorithms/Project.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import path from 'path';
import Project from '@dbux/projects/src/projectLib/Project';
import { buildJestRunBugCommand } from '@dbux/projects/src/util/jestUtil';
import Project from '../../projectLib/Project';
import { buildJestRunBugCommand } from '../../util/jestUtil';

export default class JavascriptAlgorithmProject extends Project {
gitRemote = 'trekhleb/javascript-algorithms.git';

rmFiles = [
'.babelrc', // we need .babelrc.js instead
'.huskyrc.json' // unwanted commit hooks
];

loadBugs() {
// TODO: load automatically from BugsJs bug database
// NOTE: some bugs have multiple test files, or no test file at all
Expand Down Expand Up @@ -47,13 +51,22 @@ export default class JavascriptAlgorithmProject extends Project {
// nothing to do here
}

async testBugCommand(/* bug, cfg */) {
// TODO: copy correct version from express/Project.js
async testBugCommand(bug, cfg) {
const { projectPath } = this;
// const bugArgs = this.getMochaRunArgs(bug);
const bugConfig = this.getJestCfg(bug, [
'--setupFilesAfterEnv ./dbuxJestSetup.js',
'--testTimeout 30000' // timeout
]);

// const { projectPath } = this;
// const bugArgs = this.getBugArgs(bug);
const mochaCfg = {
...cfg,
...bugConfig,
dbuxJs: null,
cwd: projectPath,
};

// const jestArgs = `${bugArgs}`;
// return buildJestRunBugCommand(projectPath, jestArgs, bug.require, debugPort);
// node --stack-trace-limit=100 "./node_modules/jest/bin/jest.js" --runInBand -t "BubbleSort should sort array" --runTestsByPath src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js --cache=false
return buildJestRunBugCommand(mochaCfg);
}
}
4 changes: 2 additions & 2 deletions dbux-projects/src/projects/karma/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ export default class KarmaProject extends Project {

async testBugCommand(bug, cfg) {
const { projectPath } = this;
const bugArgs = this.getMochaRunArgs(bug, [
const testArgs = this.getMochaRunArgs(bug, [
'-t 10000' // timeout
]);

const { nodeVersion } = this; // TODO

const mochaCfg = {
cwd: projectPath,
mochaArgs: bugArgs,
testArgs,
require: [
...(bug.require || EmptyArray),
this.manager.getDbuxPath(`@dbux/runtime/deps/require.ws.${nodeVersion}.js`)
Expand Down
35 changes: 30 additions & 5 deletions dbux-projects/src/util/jestUtil.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
import EmptyArray from '@dbux/common/src/util/EmptyArray';
import { buildNodeCommand } from './nodeUtil';

/**
* @see https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest
*/
export async function buildJestRunBugCommand(cwd, jestArgs, requireArr, debugPort = 9309) {
const program = `${cwd}/node_modules/jest/bin/jest.js`;
export async function buildJestRunBugCommand(cfg) {
let {
cwd,
dbuxJs,
testArgs = '',
// keepAlive = true, // TODO: keep alive
nodeArgs,
dbuxArgs,
require = EmptyArray,
debugPort
} = cfg;

const jestJs = `${cwd}/node_modules/jest/bin/jest.js`;

// const transform = `{
// "^.+\\.jsx?$": ["babel-jest"]
// }`;
jestArgs = `${jestArgs} --cache=false`;
testArgs = `${testArgs} --cache=false`;
// --transform=${transform}`;
// --cache=false

// final command

let program;
let programArgs;
if (dbuxJs) {
program = dbuxJs;
programArgs = `run ${dbuxArgs} "${jestJs}" -- ${testArgs}`;
}
else {
program = jestJs;
programArgs = testArgs;
}

return buildNodeCommand({
cwd,
nodeArgs,
debugPort,
require,
program,
require: requireArr,
programArgs: `${jestArgs}`
programArgs
});
}
10 changes: 5 additions & 5 deletions dbux-projects/src/util/mochaUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function buildMochaRunCommand(cfg) {
let {
cwd,
dbuxJs,
mochaArgs = '-c', // colors
testArgs = '-c', // colors
keepAlive = true,
nodeArgs,
dbuxArgs,
Expand All @@ -19,7 +19,7 @@ export async function buildMochaRunCommand(cfg) {
// keep alive: if we don't do this, mocha will call `process.exit` when run has ended, and we won't receive data sent by runtime
const noExit = keepAlive ? '--no-exit ' : '';

mochaArgs = `${noExit}${mochaArgs}`;
testArgs = `${noExit}${testArgs}`;

// NOTE: `Project.installDbuxDependencies` installs @dbux/cli for us

Expand All @@ -35,19 +35,19 @@ export async function buildMochaRunCommand(cfg) {
// debugPort,
// program: mochaJs,
// require,
// programArgs: `${keepAlive} ${mochaArgs}`
// programArgs: `${keepAlive} ${testArgs}`
// });

const mochaJs = `${cwd}/node_modules/mocha/bin/_mocha`;
let program;
let programArgs;
if (dbuxJs) {
program = dbuxJs;
programArgs = `run ${dbuxArgs} "${mochaJs}" -- ${mochaArgs}`;
programArgs = `run ${dbuxArgs} "${mochaJs}" -- ${testArgs}`;
}
else {
program = mochaJs;
programArgs = mochaArgs;
programArgs = testArgs;
}

return buildNodeCommand({
Expand Down
2 changes: 2 additions & 0 deletions dbux-runtime/src/ProgramMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default class ProgramMonitor {
this._staticProgramContext = staticProgramContext;
this._programContextId = this.pushImmediate(inProgramStaticContextId, ProgramStartTraceId, false);
this._logger = newLogger(staticProgramContext.filePath);

// this._logger.debug(`Started tracing program...`);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dbux-runtime/src/RuntimeMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export default class RuntimeMonitor {
// ###########################################################################

disabled = 0;
tracesDisabled = 1;
tracesDisabled = 0;

incDisabled() {
++this.disabled;
Expand Down
Loading