Skip to content

Commit a3733d0

Browse files
authored
feat(esm): @cedarjs/testing dual ESM/CJS build (#163)
The testing package is a bit weird in that it has both a `src/` directory and a `config/` directory with code. Plus `api/`, `cache/` and `/web` with just an index file and a package.json. I know what to do with the `src/` directory - same as I've done with all the other ESM/CJS conversions. But not sure what to do with the rest of the directories yet. ⚠️ The goal is that this change should be non-breakable, but if you're doing dist imports from `@cedarjs/testing` it might be. Please let me know in that case and I'll fix it.
1 parent e8a3ed8 commit a3733d0

30 files changed

+192
-54
lines changed

.changesets/163.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- feat(esm): `@cedarjs/testing` dual ESM/CJS build (#163) by @Tobbe
2+
3+
This PR allows both CJS and ESM projects to use the `@cedarjs/testing` package.
4+
Among other things it does this by adding `exports` to the package.json file.
5+
6+
⚠️ The goal is that this change should be non-breakable, but if you're doing
7+
dist imports from `@cedarjs/testing` it might be. Please let me know in that
8+
case and I'll fix it.

packages/storybook/src/mocks/MockProviders.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import React from 'react'
66

77
import { LocationProvider } from '@cedarjs/router'
8-
import { useAuth } from '@cedarjs/testing/web'
8+
import { useAuth } from '@cedarjs/testing/auth'
99
import { RedwoodProvider } from '@cedarjs/web'
1010
import { RedwoodApolloProvider } from '@cedarjs/web/apollo'
1111

packages/storybook/src/plugins/mock-auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function mockAuth(): PluginOption {
1515
)
1616
// Add import to mocked `createAuth` at the top of the file.
1717
code =
18-
"import { createAuthentication as createAuth } from '@cedarjs/testing/dist/web/mockAuth.js'\n" +
18+
"import { createAuthentication as createAuth } from '@cedarjs/testing/auth'\n" +
1919
code
2020
}
2121
return code

packages/storybook/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"compilerOptions": {
55
"outDir": "./dist",
66
"rootDir": "./src",
7-
"types": ["node", "vite/client"]
7+
"types": ["node", "vite/client"],
8+
"paths": {
9+
"@cedarjs/testing/auth": ["../testing/dist/web/mockAuth.d.ts"],
10+
"@cedarjs/testing/web": ["../testing/dist/web/index.d.ts"]
11+
}
812
},
913
"include": ["src/**/*"]
1014
}

packages/testing/api/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/testing/api/package.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/testing/build.mts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1-
import { build } from '@cedarjs/framework-tools'
1+
import fs from 'node:fs'
22

3-
await build()
3+
import { buildCjs, buildEsm } from '@cedarjs/framework-tools'
4+
import {
5+
generateTypesCjs,
6+
generateTypesEsm,
7+
insertCommonJsPackageJson,
8+
} from '@cedarjs/framework-tools/generateTypes'
9+
10+
await buildEsm()
11+
await generateTypesEsm()
12+
13+
await buildCjs()
14+
await generateTypesCjs()
15+
await insertCommonJsPackageJson({
16+
buildFileUrl: import.meta.url,
17+
})
18+
19+
// ./src/web/mockRequests.js contains `... = await import('msw/node'`. When
20+
// building for CJS esbuild correctly preserves the `await import` statement
21+
// because it's valid in both CJS and ESM (whereas regular imports are only
22+
// valid in ESM).
23+
// The problem is that this file will be consumed by Jest, and jest doesn't
24+
// support that syntax. They only support `require()`.
25+
// That's why we have to do manual editing of built files here
26+
const mockRequestsBuildPath = './dist/cjs/web/mockRequests.js'
27+
const mockRequestsFile = fs.readFileSync(mockRequestsBuildPath, 'utf-8')
28+
fs.writeFileSync(
29+
mockRequestsBuildPath,
30+
mockRequestsFile.replaceAll('await import', 'require'),
31+
)

packages/testing/cache/index.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/testing/cache/package.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/testing/config/jest/api/jest.setup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// @NOTE without these imports in the setup file, mockCurrentUser
55
// will remain undefined in the user's tests
66
// Remember to use specific imports
7-
const { defineScenario } = require('@cedarjs/testing/dist/api/scenario')
7+
const { defineScenario } = require('@cedarjs/testing/dist/cjs/api/scenario')
88

99
// @NOTE we do this because jest.setup.js runs every time in each context
1010
// while jest-preset runs once. This significantly reduces memory footprint, and testing time

0 commit comments

Comments
 (0)