Skip to content

Commit

Permalink
[test] Properly wait for promise to resolve before expect (#3597)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw authored Jan 12, 2022
1 parent e56c38b commit 84383bc
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const generateDate = (
return rawDate.getTime();
};

const nativeSetTimeout = setTimeout;

// TODO: Replace `cell.focus()` with `fireEvent.mouseUp(cell)`
describe('<DataGridPro /> - Edit Rows', () => {
let baselineProps;
Expand Down Expand Up @@ -1541,13 +1543,13 @@ describe('<DataGridPro /> - Edit Rows', () => {
fireEvent.doubleClick(cell);
const input = cell.querySelector('input')!;
fireEvent.change(input, { target: { value: 'Adidas' } });
await clock.runToLast();
clock.runToLast();
await new Promise((resolve) => nativeSetTimeout(resolve));
expect(apiRef.current.getEditRowsModel()[0].brand.value).to.equal('Adidas');
fireEvent.keyDown(input, { key: 'Enter' });
await waitFor(() => {
expect(cell).not.to.have.class('MuiDataGrid-cell--editing');
expect(cell).to.have.text('Adidas');
});
await new Promise((resolve) => nativeSetTimeout(resolve));
expect(cell).not.to.have.class('MuiDataGrid-cell--editing');
expect(cell).to.have.text('Adidas');
});
});

Expand Down
124 changes: 124 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const childProcess = require('child_process');
const fs = require('fs');
const glob = require('fast-glob');
const path = require('path');
const yargs = require('yargs');

async function run(argv) {
const workspaceRoot = path.resolve(__dirname, '../');

const gitignore = fs.readFileSync(path.join(workspaceRoot, '.gitignore'), { encoding: 'utf-8' });
const ignore = gitignore
.split(/\r?\n/)
.filter((pattern) => {
return pattern.length > 0 && !pattern.startsWith('#');
})
.map((line) => {
if (line.startsWith('/')) {
// "/" marks the cwd of the ignore file.
// Since we declare the dirname of the gitignore the cwd we can prepend "." as a shortcut.
return `.${line}`;
}
return line;
});
const globPattern = `**/*${argv.testFilePattern.replace(/\\/g, '/')}*`;
const spec = glob
.sync(globPattern, {
cwd: workspaceRoot,
ignore,
})
.filter((relativeFile) => {
return /\.test\.(js|ts|tsx)$/.test(relativeFile);
});

if (spec.length === 0) {
throw new Error(`Could not find any file test files matching '${globPattern}'`);
}

const args = ['mocha'].concat(spec);
if (argv.bail) {
args.push('--bail');
}
if (argv.debug || argv.inspecting) {
args.push('--timeout 0');
}
if (argv.debug) {
args.push('--inspect-brk');
}
if (!argv.single) {
args.push('--watch');
}
if (argv.testNamePattern !== undefined) {
args.push(`--grep '${argv.testNamePattern}'`);
}

const mochaProcess = childProcess.spawn('yarn', args, {
env: {
...process.env,
BABEL_ENV: 'test',
NODE_ENV: argv.production ? 'production' : 'test',
},
shell: true,
stdio: ['inherit', 'inherit', 'inherit'],
});

mochaProcess.once('exit', (signal) => {
process.exit(signal !== null ? signal : undefined);
});

process.on('SIGINT', () => {
// Forward interrupt.
// Otherwise cli.js exits and the you get dangling console output from mocha.
// "dangling" meaning that you get mocha output in the new terminal input.
mochaProcess.kill('SIGINT');
});
}

yargs
.command({
command: '$0 <testFilePattern>',
description: 'Test cli for developing',
builder: (command) => {
return command
.positional('testFilePattern', {
description: 'Only test files match "**/*{testFilePattern}*.test.{js,ts,tsx}"',
type: 'string',
})
.option('bail', {
alias: 'b',
description: 'Stop on first error.',
type: 'boolean',
})
.option('debug', {
alias: 'd',
description:
'Allows attaching a debugger and waits for the debugger to start code execution.',
type: 'boolean',
})
.option('inspecting', {
description: 'In case you expect to hit breakpoints that may interrupt a test.',
type: 'boolean',
})
.option('production', {
alias: 'p',
description:
'Run tests in production environment. WARNING: Will not work with most tests.',
type: 'boolean',
})
.option('single', {
alias: 's',
description: 'Run only once i.e. not in watch mode.',
type: 'boolean',
})
.option('testNamePattern', {
alias: 't',
description: 'Limit tests by their name given a pattern.',
type: 'string',
});
},
handler: run,
})
.help()
.strict(true)
.version(false)
.parse();

0 comments on commit 84383bc

Please sign in to comment.