|
| 1 | +import { load } from 'cheerio' |
| 2 | +import { getLogger } from 'lambda-local' |
| 3 | +import { cp } from 'node:fs/promises' |
| 4 | +import { HttpResponse, http, passthrough } from 'msw' |
| 5 | +import { setupServer } from 'msw/node' |
| 6 | +import { v4 } from 'uuid' |
| 7 | +import { Mock, afterAll, afterEach, beforeAll, beforeEach, expect, test, vi } from 'vitest' |
| 8 | +import { type FixtureTestContext } from '../utils/contexts.js' |
| 9 | +import { createFixture, invokeFunction, runPlugin } from '../utils/fixture.js' |
| 10 | +import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js' |
| 11 | + |
| 12 | +vi.mock('node:fs/promises', async (importOriginal) => { |
| 13 | + const fsPromisesModule = (await importOriginal()) as typeof import('node:fs/promises') |
| 14 | + return { |
| 15 | + ...fsPromisesModule, |
| 16 | + cp: vi.fn(fsPromisesModule.cp.bind(fsPromisesModule)), |
| 17 | + } |
| 18 | +}) |
| 19 | + |
| 20 | +let server: ReturnType<typeof setupServer> |
| 21 | + |
| 22 | +// Disable the verbose logging of the lambda-local runtime |
| 23 | +getLogger().level = 'alert' |
| 24 | + |
| 25 | +const purgeAPI = vi.fn() |
| 26 | + |
| 27 | +beforeAll(() => { |
| 28 | + server = setupServer( |
| 29 | + http.post('https://api.netlify.com/api/v1/purge', async ({ request }) => { |
| 30 | + purgeAPI(await request.json()) |
| 31 | + |
| 32 | + return HttpResponse.json({ |
| 33 | + ok: true, |
| 34 | + }) |
| 35 | + }), |
| 36 | + http.all(/.*/, () => passthrough()), |
| 37 | + ) |
| 38 | + server.listen() |
| 39 | +}) |
| 40 | + |
| 41 | +afterAll(() => { |
| 42 | + // Disable API mocking after the tests are done. |
| 43 | + server.close() |
| 44 | +}) |
| 45 | + |
| 46 | +beforeEach<FixtureTestContext>(async (ctx) => { |
| 47 | + // set for each test a new deployID and siteID |
| 48 | + ctx.deployID = generateRandomObjectID() |
| 49 | + ctx.siteID = v4() |
| 50 | + vi.stubEnv('SITE_ID', ctx.siteID) |
| 51 | + vi.stubEnv('DEPLOY_ID', ctx.deployID) |
| 52 | + // hide debug logs in tests |
| 53 | + vi.spyOn(console, 'debug').mockImplementation(() => {}) |
| 54 | + |
| 55 | + purgeAPI.mockClear() |
| 56 | + |
| 57 | + await startMockBlobStore(ctx) |
| 58 | +}) |
| 59 | + |
| 60 | +afterEach(() => { |
| 61 | + vi.unstubAllEnvs() |
| 62 | +}) |
| 63 | + |
| 64 | +test<FixtureTestContext>('Test that the hello-world-turbopack next app is working', async (ctx) => { |
| 65 | + await createFixture('hello-world-turbopack', ctx) |
| 66 | + await runPlugin(ctx) |
| 67 | + |
| 68 | + // test the function call |
| 69 | + const home = await invokeFunction(ctx) |
| 70 | + expect(home.statusCode).toBe(200) |
| 71 | + expect(load(home.body)('h1').text()).toBe('Hello, Next.js!') |
| 72 | +}) |
0 commit comments