Skip to content

Commit f4def66

Browse files
authored
fix(data-migrate): Revert "chore(dataMigrate): Switch from jest to vitest (#11978)" (#135)
This reverts commit f16f0dd. When I switched from jest to vitest I also switched from `require(...)` to `await import(...)`. That also meant not running babel anymore, which we actually need for TS support. So this PR reverts back to `require` (and jest) to get TS data migrations working again Fixes #132
1 parent 0b83288 commit f4def66

File tree

10 files changed

+174
-159
lines changed

10 files changed

+174
-159
lines changed

packages/cli-packages/dataMigrate/dist.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import fs from 'node:fs'
2-
import path from 'node:path'
3-
4-
import { describe, expect, it } from 'vitest'
1+
import fs from 'fs'
2+
import path from 'path'
53

64
const distPath = path.join(__dirname, 'dist')
75

packages/cli-packages/dataMigrate/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
"build:types": "tsc --build --verbose",
2222
"prepublishOnly": "NODE_ENV=production yarn build",
2323
"test": "yarn test:unit && yarn test:dist",
24-
"test:dist": "yarn vitest run ./dist.test.ts",
25-
"test:unit": "yarn vitest run src/"
24+
"test:dist": "yarn jest ./dist.test.ts",
25+
"test:unit": "yarn jest src"
2626
},
2727
"dependencies": {
2828
"@cedarjs/babel-config": "workspace:*",
@@ -40,10 +40,10 @@
4040
"@prisma/client": "5.20.0",
4141
"@types/fs-extra": "11.0.4",
4242
"@types/yargs": "17.0.33",
43+
"jest": "29.7.0",
4344
"memfs": "4.17.2",
4445
"tsx": "4.19.4",
45-
"typescript": "5.6.2",
46-
"vitest": "2.1.9"
46+
"typescript": "5.6.2"
4747
},
4848
"gitHead": "3905ed045508b861b495f8d5630d76c7a157d8f1"
4949
}

packages/cli-packages/dataMigrate/src/__tests__/install.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { vi, describe, expect, it } from 'vitest'
2-
import type { Argv } from 'yargs'
1+
import type yargs from 'yargs'
32

43
import * as installCommand from '../commands/install'
54
import { handler as dataMigrateInstallHandler } from '../commands/installHandler.js'
65

7-
vi.mock('../commands/installHandler.js', () => ({
8-
handler: vi.fn(),
9-
}))
6+
jest.mock(
7+
'../commands/installHandler.js',
8+
() => ({
9+
handler: jest.fn(),
10+
}),
11+
{ virtual: true },
12+
)
1013

1114
describe('install', () => {
1215
it('exports `command`, `description`, `builder`, and `handler`', () => {
@@ -25,7 +28,7 @@ describe('install', () => {
2528
it('`builder` has an epilogue', () => {
2629
// The typecasting here is to make TS happy when calling `builder(yargs)`
2730
// further down. We know that only `epilogue` will be called.
28-
const yargs = { epilogue: vi.fn() } as unknown as Argv
31+
const yargs = { epilogue: jest.fn() } as unknown as yargs.Argv
2932

3033
installCommand.builder(yargs)
3134

packages/cli-packages/dataMigrate/src/__tests__/installHandler.test.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import fs from 'fs'
2+
13
import execa from 'execa'
2-
import { vol, fs as memfs } from 'memfs'
3-
import { vi, describe, expect, it } from 'vitest'
4+
import { vol } from 'memfs'
45

56
import { getPaths } from '@cedarjs/project-config'
67

@@ -11,18 +12,15 @@ import {
1112
notes,
1213
} from '../commands/installHandler'
1314

14-
vi.mock('fs', async () => ({ ...memfs, default: { ...memfs } }))
15-
vi.mock('node:fs', async () => ({ ...memfs, default: { ...memfs } }))
15+
jest.mock('fs', () => require('memfs').fs)
1616

17-
vi.mock('execa', () => {
17+
jest.mock('execa', () => {
1818
return {
19-
default: {
20-
command: vi.fn(() => {
21-
return {
22-
stdout: 42,
23-
}
24-
}),
25-
},
19+
command: jest.fn(() => {
20+
return {
21+
stdout: 42,
22+
}
23+
}),
2624
}
2725
})
2826

@@ -60,14 +58,14 @@ describe('installHandler', () => {
6058
redwoodProjectPath,
6159
)
6260

63-
console.log = vi.fn()
61+
console.log = jest.fn()
6462

6563
await handler()
6664

6765
const dataMigrationsPath = getPaths().api.dataMigrations
6866

69-
expect(memfs.readdirSync(dataMigrationsPath)).toEqual(['.keep'])
70-
expect(memfs.readFileSync(getPaths().api.dbSchema, 'utf-8')).toMatch(
67+
expect(fs.readdirSync(dataMigrationsPath)).toEqual(['.keep'])
68+
expect(fs.readFileSync(getPaths().api.dbSchema, 'utf-8')).toMatch(
7169
RW_DATA_MIGRATION_MODEL,
7270
)
7371
expect(execa.command).toHaveBeenCalledWith(createDatabaseMigrationCommand, {

packages/cli-packages/dataMigrate/src/__tests__/up.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import { fs as memfs, vol } from 'memfs'
2-
import { vi, describe, expect, it } from 'vitest'
3-
import yargs from 'yargs'
1+
import { vol } from 'memfs'
2+
import yargs from 'yargs/yargs'
43

54
import { getPaths } from '@cedarjs/project-config'
65

76
import * as upCommand from '../commands/up'
87
import { handler as dataMigrateUpHandler } from '../commands/upHandler.js'
98

10-
vi.mock('fs', () => ({ default: memfs }))
11-
vi.mock('../commands/upHandler.js', () => ({
12-
handler: vi.fn(),
13-
}))
9+
jest.mock('fs', () => require('memfs').fs)
10+
jest.mock(
11+
'../commands/upHandler.js',
12+
() => ({
13+
handler: jest.fn(),
14+
}),
15+
{ virtual: true },
16+
)
1417

1518
describe('up', () => {
1619
it('exports `command`, `description`, `builder`, and `handler`', () => {
@@ -36,17 +39,14 @@ describe('up', () => {
3639

3740
process.env.RWJS_CWD = '/redwood-app'
3841

39-
const { argv } = upCommand.builder(yargs())
42+
const { argv } = upCommand.builder(yargs)
4043

4144
expect(argv).toHaveProperty('import-db-client-from-dist', false)
4245
expect(argv).toHaveProperty('dist-path', getPaths().api.dist)
4346
})
4447

4548
it('`handler` proxies to `./upHandler.js`', async () => {
46-
await upCommand.handler({
47-
importDbClientFromDist: false,
48-
distPath: '',
49-
})
49+
await upCommand.handler({})
5050
expect(dataMigrateUpHandler).toHaveBeenCalled()
5151
})
5252
})

0 commit comments

Comments
 (0)