astro/packages/integrations/node/test/locals.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';
describe('API routes', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/locals/',
output: 'server',
adapter: nodejs({ mode: 'middleware' }),
});
await fixture.build();
});
it('Can render locals in page', async () => {
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
let { req, res, text } = createRequestAndResponse({
method: 'POST',
url: '/foo',
});
let locals = { foo: 'bar' };
handler(req, res, () => {}, locals);
req.send();
let html = await text();
expect(html).to.contain('<h1>bar</h1>');
});
it('Can access locals in API', async () => {
const { handler } = await import('./fixtures/locals/dist/server/entry.mjs');
let { req, res, done } = createRequestAndResponse({
method: 'POST',
url: '/api',
});
let locals = { foo: 'bar' };
handler(req, res, () => {}, locals);
req.send();
let [buffer] = await done;
let json = JSON.parse(buffer.toString('utf-8'));
expect(json.foo).to.equal('bar');
});
});