Skip to content

Commit

Permalink
Compare/debug test implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
codersmith committed Feb 8, 2024
1 parent 8295a9a commit 0b9dc57
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"@babel/cli": "7.23.9",
"@babel/core": "^7.22.20",
"@types/archiver": "^6",
"jest": "29.7.0",
"memfs": "4.6.0",
"typescript": "5.3.3",
"vitest": "1.2.2"
Expand Down
82 changes: 82 additions & 0 deletions packages/cli/src/commands/__tests__/execHandler-jest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { vol } from 'memfs'

import { runScriptFunction as mockRunScriptFunction } from '../../lib/exec'
import { generatePrismaClient as mockGeneratePrismaClient } from '../../lib/generatePrismaClient'
import { handler } from '../execHandler'

const redwoodProjectPath = '/redwood-app'
process.env.RWJS_CWD = redwoodProjectPath

jest.mock('fs', () => require('memfs').fs)
jest.mock('../../lib/exec')
jest.mock('../../lib/generatePrismaClient', () => {
return {
generatePrismaClient: jest.fn().mockResolvedValue(true),
}
})
jest.mock('listr2', () => {
return {
// Return a constructor function, since we're calling `new` on Listr
Listr: jest.fn().mockImplementation((tasks) => {
return {
run: async () => {
for (const task of tasks) {
const skip =
typeof task.skip === 'function' ? task.skip : () => task.skip

if (!skip()) {
await task.task()
}
}
},
}
}),
}
})

jest.spyOn(console, 'log').mockImplementation(() => {})
jest.spyOn(console, 'info').mockImplementation(() => {})
jest.spyOn(console, 'warn').mockImplementation(() => {})


describe('execHandler.js', () => {
beforeEach(() => {
vol.reset()
jest.clearAllMocks()
})

afterEach(() => {
vol.reset()
jest.clearAllMocks()
})

afterAll(() => {
jest.restoreAllMocks()
})

describe('handler', () => {
it('resolves script without extension', async () => {
const fooScript =
'export default async ({ args }) => { console.log(`:: Executing script ${__filename} ::`) }'

vol.fromNestedJSON(
{
'redwood.toml': '',
scripts: {
'foo.js': fooScript,
},
},
redwoodProjectPath
)

await handler({ name: 'foo', prisma: true, arg1: 'a1', arg2: 'a2' })

expect(mockGeneratePrismaClient).toHaveBeenCalledWith({ force: false })
expect(mockRunScriptFunction).toHaveBeenCalledWith({
path: '/redwood-app/scripts/foo',
functionName: 'default',
args: { args: { arg1: 'a1', arg2: 'a2' } },
})
})
})
})
103 changes: 103 additions & 0 deletions packages/cli/src/commands/__tests__/execHandler-vitest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { vol } from 'memfs'
import {
vi,
afterAll,
afterEach,
beforeEach,
describe,
expect,
it,
} from 'vitest'

import { runScriptFunction as mockRunScriptFunction } from '../../lib/exec'
import { generatePrismaClient as mockGeneratePrismaClient } from '../../lib/generatePrismaClient'
import { handler } from '../execHandler'

const redwoodProjectPath = '/redwood-app'
process.env.RWJS_CWD = redwoodProjectPath

vi.mock('@redwoodjs/project-config', () => {
const path = require('path')
const BASE_PATH = '/redwood-app'
return {
resolveFile: (path) => path,
getPaths: () => {
return {
base: BASE_PATH,
scripts: path.join(BASE_PATH, 'scripts'),
}
},
}
})
vi.mock('fs', () => require('memfs').fs)
vi.mock('../../lib/exec')
vi.mock('../../lib/generatePrismaClient', () => {
return {
generatePrismaClient: vi.fn().mockResolvedValue(true),
}
})
vi.mock('listr2', () => {
return {
// Return a constructor function, since we're calling `new` on Listr
Listr: vi.fn().mockImplementation((tasks) => {
return {
run: async () => {
for (const task of tasks) {
const skip =
typeof task.skip === 'function' ? task.skip : () => task.skip

if (!skip()) {
await task.task()
}
}
},
}
}),
}
})

vi.spyOn(console, 'log').mockImplementation(() => {})
vi.spyOn(console, 'info').mockImplementation(() => {})
vi.spyOn(console, 'warn').mockImplementation(() => {})

describe('execHandler.js', () => {
beforeEach(() => {
vol.reset()
vi.clearAllMocks()
})

afterEach(() => {
vol.reset()
vi.clearAllMocks()
})

afterAll(() => {
vi.restoreAllMocks()
})

describe('handler', () => {
it('resolves script without extension', async () => {
const fooScript =
'export default async ({ args }) => { console.log(`:: Executing script ${__filename} ::`) }'

vol.fromNestedJSON(
{
'redwood.toml': '',
scripts: {
'foo.js': fooScript,
},
},
redwoodProjectPath
)

await handler({ name: 'foo', prisma: true, arg1: 'a1', arg2: 'a2' })

expect(mockGeneratePrismaClient).toHaveBeenCalledWith({ force: false })
expect(mockRunScriptFunction).toHaveBeenCalledWith({
path: '/redwood-app/scripts/foo',
functionName: 'default',
args: { args: { arg1: 'a1', arg2: 'a2' } },
})
})
})
})
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8113,6 +8113,7 @@ __metadata:
fs-extra: "npm:11.2.0"
fs-monkey: "npm:^1.0.5"
humanize-string: "npm:2.1.0"
jest: "npm:29.7.0"
jscodeshift: "npm:0.15.0"
latest-version: "npm:5.1.0"
listr2: "npm:6.6.1"
Expand Down

0 comments on commit 0b9dc57

Please sign in to comment.