Skip to content

Commit

Permalink
Use Jest projects (#1294)
Browse files Browse the repository at this point in the history
* use jest projects

* poc

* tweak ordering

* Add changeset

* Nit

* remove extra export

* update changeset

* Tweak changeset

* Update src/api/jest/index.ts

Co-authored-by: Ryan Ling <ryan@outlook.com.au>

---------

Co-authored-by: Ryan Ling <ryan@outlook.com.au>
  • Loading branch information
samchungy and 72636c authored Oct 11, 2023
1 parent 93a431a commit 2d8b57a
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 22 deletions.
55 changes: 55 additions & 0 deletions .changeset/breezy-bees-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
'skuba': minor
---

Jest.mergePreset: Propagate root-level configuration options to `projects`

[`Jest.mergePreset`](https://seek-oss.github.io/skuba/docs/development-api/jest.html#mergepreset) now propagates the `moduleNameMapper` and `transform` options from root-level configuration to the `projects` array.

If you were referencing the base config in the `projects` array:

```ts
const baseConfig = Jest.mergePreset({
// ...
});

export default {
...baseConfig,
projects: [
{
...baseConfig,
displayName: 'unit',
setupFiles: ['<rootDir>/jest.setup.ts'],
testPathIgnorePatterns: ['\\.int\\.test\\.ts'],
},
{
...baseConfig,
displayName: 'integration',
setupFiles: ['<rootDir>/jest.setup.ts'],
testMatch: ['**/*.int.test.ts'],
},
],
};
```

You can replace it with the following:

```ts
export default Jest.mergePreset({
// ...
projects: [
{
displayName: 'unit',
setupFiles: ['<rootDir>/jest.setup.ts'],
testPathIgnorePatterns: ['\\.int\\.test\\.ts'],
},
{
displayName: 'integration',
setupFiles: ['<rootDir>/jest.setup.ts'],
testMatch: ['**/*.int.test.ts'],
},
],
});
```

The `projects` option allows you to reuse a single Jest config file for different test types. View the [Jest documentation](https://jestjs.io/docs/configuration#projects-arraystring--projectconfig) for more information.
11 changes: 0 additions & 11 deletions jest.config.int.ts

This file was deleted.

24 changes: 20 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@ export default Jest.mergePreset({
coveragePathIgnorePatterns: ['<rootDir>/integration/', '<rootDir>/template/'],
displayName: 'unit',
setupFiles: ['<rootDir>/jest.setup.ts'],
testPathIgnorePatterns: [
'<rootDir>/template/',
'\\.int\\.test\\.ts',
'/test\\.ts',
testPathIgnorePatterns: ['<rootDir>/template/', '/test\\.ts'],
watchPathIgnorePatterns: [
'<rootDir>/integration/format/',
'<rootDir>/integration/lint/',
],
projects: [
{
displayName: 'unit',
setupFiles: ['<rootDir>/jest.setup.ts'],
testPathIgnorePatterns: [
'<rootDir>/template/',
'/test\\.ts',
'\\.int\\.test\\.ts',
],
},
{
displayName: 'integration',
setupFiles: ['<rootDir>/jest.setup.ts'],
testMatch: ['**/*.int.test.ts'],
},
],
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
"release": "yarn build && changeset publish",
"skuba": "yarn build && node lib/skuba",
"stage": "changeset version && yarn format",
"test": "yarn skuba test",
"test:ci": "yarn skuba test --config jest.config.int.ts --runInBand",
"test:int": "yarn skuba test --config jest.config.int.ts --runInBand",
"test": "yarn skuba test --selectProjects unit",
"test:ci": "yarn skuba test --runInBand",
"test:int": "yarn skuba test --selectProjects integration --runInBand",
"test:template": "scripts/test-template.sh",
"test:watch": "yarn skuba test --config jest.config.int.ts --runInBand --watch"
"test:watch": "yarn skuba test --runInBand --watch"
},
"remarkConfig": {
"plugins": [
Expand Down
27 changes: 27 additions & 0 deletions src/api/jest/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,31 @@ describe('mergePreset', () => {

expect(config).toHaveProperty('testEnvironment', 'jsdom');
});

it('provides the jest preset transform and moduleNameMapper to projects', () => {
const config = mergePreset({
projects: [{ displayName: 'unit' }, { displayName: 'integration' }],
});

expect(config.projects).toStrictEqual([
{
displayName: 'unit',
transform: expect.any(Object),
moduleNameMapper: expect.any(Object),
},
{
displayName: 'integration',
transform: expect.any(Object),
moduleNameMapper: expect.any(Object),
},
]);
});

it('allows paths to be passed to projects', () => {
const config = mergePreset({
projects: ['src/project1', 'src/project2'],
});

expect(config.projects).toStrictEqual(['src/project1', 'src/project2']);
});
});
28 changes: 25 additions & 3 deletions src/api/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ type DefaultOptions =
*/
export const mergePreset = <
AdditionalOptions extends keyof Config.InitialOptions,
>(
options: Pick<Config.InitialOptions, AdditionalOptions | DefaultOptions>,
): Config.InitialOptions => mergeRaw(jestPreset, options);
>({
projects,
...options
}: Pick<
Config.InitialOptions,
AdditionalOptions | DefaultOptions
>): Config.InitialOptions => {
const root = mergeRaw(jestPreset, options);

return {
...root,

projects: projects?.map((project) => {
if (typeof project === 'string') {
return project;
}

return {
moduleNameMapper: root.moduleNameMapper,
transform: root.transform,
...project,
};
}),
};
};

0 comments on commit 2d8b57a

Please sign in to comment.