Skip to content

Commit

Permalink
jest option to bypass modular behaviour (#2363)
Browse files Browse the repository at this point in the history
* add modular test bypass flag

* update test documentation
  • Loading branch information
AlbertoBrusa committed Apr 27, 2023
1 parent ccffbfd commit d770c80
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-rings-hunt.md
@@ -0,0 +1,5 @@
---
'modular-scripts': minor
---

Added --jest flag to bypass modular test behaviour
@@ -1,5 +1,5 @@
import add from '../index';

test('it should add two numbers', () => {
test('it should add two numbers unique', () => {
expect(add(5, 5)).toEqual(10);
});
19 changes: 19 additions & 0 deletions docs/commands/test.md
Expand Up @@ -59,6 +59,10 @@ empty, Modular will write a message to `stdout` and exit with code `0`.

### Modular-specific options

`--bypass`: Bypass Modular selective behaviour and flags, and send all provided
flags and options directly to the Jest process with Modular configuration.
Useful when running `modular test` from IntelliJ.

`--ancestors`: Take the packages specified by the user via arguments or options
and add their ancestors (i.e. the packages that have a direct or indirect
dependency on them) to the test list.
Expand Down Expand Up @@ -93,6 +97,21 @@ underlying Jest process.
`modular test` additionally supports passing
[Jest CLI options](https://jestjs.io/docs/cli) to the underlying Jest process.

## Configuring IntelliJ

IntelliJ and `modular test` aren't compatible out of the box. To make it work,
modify the IntelliJ test configuration to use `modular test` with the `--bypass`
flag.

The following is an example IntelliJ Jest configuration that uses
`modular test`:

- Jest package: `absolute_repo_path/node_modules/modular_scripts`

- Working directory: `absolute_repo_path/`

- Jest options: `test --bypass`

## Escape Hatches

While `modular test` should work straight away for most projects there are some
Expand Down
62 changes: 41 additions & 21 deletions packages/modular-scripts/src/__tests__/test.test.ts
Expand Up @@ -70,7 +70,7 @@ describe('Modular test command', () => {

let randomOutputFolder: string;

beforeEach(() => {
beforeAll(() => {
// Create random dir
randomOutputFolder = tmp.dirSync({ unsafeCleanup: true }).name;
fs.copySync(fixturesFolder, randomOutputFolder);
Expand Down Expand Up @@ -104,17 +104,16 @@ describe('Modular test command', () => {
});
});

// These expects run in a single test, serially for performance reasons (the setup time is quite long)
it('finds no unchanged using --changed / finds changed after modifying some workspaces / finds ancestors using --ancestors', () => {
const resultUnchanged = runModularPipeLogs(
it('finds no unchanged using --changed', () => {
const result = runModularPipeLogs(
randomOutputFolder,
'test --changed',
'true',
);
expect(resultUnchanged.stdout).toContain(
'No workspaces found in selection',
);
expect(result.stdout).toContain('No workspaces found in selection');
});

it('finds changed after modifying some workspaces', () => {
fs.appendFileSync(
path.join(randomOutputFolder, '/packages/b/src/index.ts'),
"\n// Comment to package b's source",
Expand All @@ -124,28 +123,49 @@ describe('Modular test command', () => {
"\n// Comment to package c's source",
);

const resultChanged = runModularPipeLogs(
const result = runModularPipeLogs(
randomOutputFolder,
'test --changed',
'true',
);
expect(resultChanged.stderr).toContain('c-nested.test.ts');
expect(resultChanged.stderr).toContain('c.test.ts');
expect(resultChanged.stderr).toContain('b-nested.test.ts');
expect(resultChanged.stderr).toContain('b.test.ts');
expect(result.stderr).toContain('c-nested.test.ts');
expect(result.stderr).toContain('c.test.ts');
expect(result.stderr).toContain('b-nested.test.ts');
expect(result.stderr).toContain('b.test.ts');
});

const resultChangedWithAncestors = runModularPipeLogs(
it('finds ancestors using --ancestors', () => {
const result = runModularPipeLogs(
randomOutputFolder,
'test --changed --ancestors',
);
expect(resultChangedWithAncestors.stderr).toContain('c-nested.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('c.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('b-nested.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('b.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('a-nested.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('a.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('e-nested.test.ts');
expect(resultChangedWithAncestors.stderr).toContain('e.test.ts');
expect(result.stderr).toContain('c-nested.test.ts');
expect(result.stderr).toContain('c.test.ts');
expect(result.stderr).toContain('b-nested.test.ts');
expect(result.stderr).toContain('b.test.ts');
expect(result.stderr).toContain('a-nested.test.ts');
expect(result.stderr).toContain('a.test.ts');
expect(result.stderr).toContain('e-nested.test.ts');
expect(result.stderr).toContain('e.test.ts');
});

it('correctly interprets Jest options when no selective options are provided', () => {
const result = runModularPipeLogs(
randomOutputFolder,
'test --testNamePattern="should add two numbers unique"',
);
expect(result.stderr).toContain(
`Ran all test suites matching /packages\\/a|packages\\/b|packages\\/c|packages\\/d|packages\\/e/i with tests matching "should add two numbers unique".`,
);
});
it('correctly interprets Jest options when selective options are provided', () => {
const result = runModularPipeLogs(
randomOutputFolder,
'test a --testNamePattern="should add two numbers unique"',
);
expect(result.stderr).toContain(
'Ran all test suites matching /packages\\/a/i with tests matching "should add two numbers unique"',
);
});
});

Expand Down
5 changes: 5 additions & 0 deletions packages/modular-scripts/src/program.ts
Expand Up @@ -173,6 +173,11 @@ interface CLITestOptions extends TestOptions {

program
.command('test [packages...]')
.option(
'--bypass',
'Bypass all modular specific selective behaviour and pass all flags and options directly to Jest. Ensures modular test behaves exactly like calling Jest directly, but with Modular configuration applied. Use with IntelliJ Jest configurations.',
false,
)
.option(
'--ancestors',
'Additionally run tests for workspaces that depend on workspaces that have changed',
Expand Down

0 comments on commit d770c80

Please sign in to comment.