This repository has been archived on 2026-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
next-deploy/packages/aws-lambda-builder/__tests__/dynamic-routes-precedence/dynamic-routes-precedence.build.test.ts
2020-07-05 16:56:58 -07:00

64 lines
1.9 KiB
TypeScript

import { join } from 'path';
import fse, { readJSON } from 'fs-extra';
import execa from 'execa';
import Builder from '../../src/build';
import { DEFAULT_LAMBDA_CODE_DIR, API_LAMBDA_CODE_DIR } from '../../src/build';
import { cleanupDir } from '../test-utils';
import { OriginRequestDefaultHandlerManifest, OriginRequestApiHandlerManifest } from '../../types';
jest.mock('execa');
describe('Dynamic Routes Precedence', () => {
let defaultBuildManifest: OriginRequestDefaultHandlerManifest;
let apiBuildManifest: OriginRequestApiHandlerManifest;
let fseRemoveSpy: jest.SpyInstance;
const fixturePath = join(__dirname, './fixture');
const outputDir = join(fixturePath, '.test_sls_next_output');
beforeEach(async () => {
const mockExeca = execa as jest.Mock;
mockExeca.mockResolvedValueOnce();
fseRemoveSpy = jest.spyOn(fse, 'remove').mockImplementation(() => {
return;
});
const builder = new Builder(fixturePath, outputDir);
await builder.build();
defaultBuildManifest = await readJSON(
join(outputDir, `${DEFAULT_LAMBDA_CODE_DIR}/manifest.json`)
);
apiBuildManifest = await readJSON(join(outputDir, `${API_LAMBDA_CODE_DIR}/manifest.json`));
});
afterEach(() => {
fseRemoveSpy.mockRestore();
return cleanupDir(outputDir);
});
it('adds dynamic page routes to the manifest in correct order of precedence', async () => {
expect.assertions(1);
const {
pages: {
ssr: { dynamic },
},
} = defaultBuildManifest;
const routes = Object.keys(dynamic);
expect(routes).toEqual(['/customers/:customer', '/:blog/:id']);
});
it('adds dynamic api routes to the manifest in correct order of precedence', async () => {
expect.assertions(1);
const {
apis: { dynamic },
} = apiBuildManifest;
const routes = Object.keys(dynamic);
expect(routes).toEqual(['/api/customers/:customer', '/api/:blog/:id']);
});
});