Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(@astrojs/node): use Node.js for testing #9758

Merged
merged 6 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/integrations/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"build:ci": "astro-scripts build \"src/**/*.ts\"",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "mocha --exit --timeout 20000 test/"
"test": "astro-scripts test \"test/**/*.test.js\""
},
"dependencies": {
"send": "^0.18.0",
Expand All @@ -45,10 +45,8 @@
"@types/server-destroy": "^1.0.3",
"astro": "workspace:*",
"astro-scripts": "workspace:*",
"chai": "^4.3.7",
"cheerio": "1.0.0-rc.12",
"express": "^4.18.2",
"mocha": "^10.2.0",
"node-mocks-http": "^1.13.0"
},
"publishConfig": {
Expand Down
13 changes: 7 additions & 6 deletions packages/integrations/node/test/api-route.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';
import crypto from 'node:crypto';
import { describe, it, before } from 'node:test';
import * as assert from 'node:assert/strict';

describe('API routes', () => {
/** @type {import('./test-utils').Fixture} */
Expand Down Expand Up @@ -33,9 +34,9 @@ describe('API routes', () => {

let json = JSON.parse(buffer.toString('utf-8'));

expect(json.length).to.equal(1);
assert.equal(json.length, 1);

expect(json[0].name).to.equal('Broccoli Soup');
assert.equal(json[0].name, 'Broccoli Soup');
});

it('Can get binary data', async () => {
Expand All @@ -54,7 +55,7 @@ describe('API routes', () => {

let [out] = await done;
let arr = Array.from(new Uint8Array(out.buffer));
expect(arr).to.deep.equal([5, 4, 3, 2, 1]);
assert.deepEqual(arr, [5, 4, 3, 2, 1]);
});

it('Can post large binary data', async () => {
Expand Down Expand Up @@ -87,7 +88,7 @@ describe('API routes', () => {
});

let [out] = await done;
expect(new Uint8Array(out.buffer)).to.deep.equal(expectedDigest);
assert.deepEqual(new Uint8Array(out.buffer), new Uint8Array(expectedDigest));
});

it('Can bail on streaming', async () => {
Expand All @@ -106,6 +107,6 @@ describe('API routes', () => {

await done;

expect(locals).to.deep.include({ cancelledByTheServer: true });
assert.deepEqual(locals, { cancelledByTheServer: true });
});
});
7 changes: 4 additions & 3 deletions packages/integrations/node/test/assets.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import * as cheerio from 'cheerio';
Expand Down Expand Up @@ -30,14 +31,14 @@ describe('Assets', () => {
it('Assets within the _astro folder should be given immutable headers', async () => {
let response = await fixture.fetch('/text-file');
let cacheControl = response.headers.get('cache-control');
expect(cacheControl).to.equal(null);
assert.equal(cacheControl, null);
const html = await response.text();
const $ = cheerio.load(html);

// Fetch the asset
const fileURL = $('a').attr('href');
response = await fixture.fetch(fileURL);
cacheControl = response.headers.get('cache-control');
expect(cacheControl).to.equal('public, max-age=31536000, immutable');
assert.equal(cacheControl, 'public, max-age=31536000, immutable');
});
});
11 changes: 7 additions & 4 deletions packages/integrations/node/test/bad-urls.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

Expand Down Expand Up @@ -32,15 +33,17 @@ describe('Bad URLs', () => {
'%20foobar%',
];

const statusCodes = [400, 404, 500];
for (const weirdUrl of weirdURLs) {
const fetchResult = await fixture.fetch(weirdUrl);
expect([400, 404, 500]).to.include(
fetchResult.status,
assert.equal(
statusCodes.includes(fetchResult.status),
true,
`${weirdUrl} returned something else than 400, 404, or 500`
);
}
const stillWork = await fixture.fetch('/');
const text = await stillWork.text();
expect(text).to.equal('<!DOCTYPE html>Hello!');
assert.equal(text, '<!DOCTYPE html>Hello!');
});
});
7 changes: 4 additions & 3 deletions packages/integrations/node/test/encoded.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';

describe('Encoded Pathname', () => {
/** @type {import('./test-utils').Fixture} */
Expand All @@ -25,7 +26,7 @@ describe('Encoded Pathname', () => {
req.send();

const html = await text();
expect(html).to.include('什么</h1>');
assert.equal(html.includes('什么</h1>'), true);
});

it('Can get a Markdown file', async () => {
Expand All @@ -39,6 +40,6 @@ describe('Encoded Pathname', () => {
req.send();

const html = await text();
expect(html).to.include('什么</h1>');
assert.equal(html.includes('什么</h1>'), true);
});
});
29 changes: 14 additions & 15 deletions packages/integrations/node/test/errors.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe('Errors', () => {
Expand All @@ -13,21 +14,19 @@ describe('Errors', () => {
});
await fixture.build();
});
describe('Within the stream', async () => {
let devPreview;
let devPreview;

before(async () => {
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('when mode is standalone', async () => {
const res = await fixture.fetch('/in-stream');
const html = await res.text();
const $ = cheerio.load(html);
before(async () => {
devPreview = await fixture.preview();
});
after(async () => {
await devPreview.stop();
});
it('when mode is standalone', async () => {
const res = await fixture.fetch('/in-stream');
const html = await res.text();
const $ = cheerio.load(html);

expect($('p').text().trim()).to.equal('Internal server error');
});
assert.equal($('p').text().trim(), 'Internal server error');
});
});
5 changes: 3 additions & 2 deletions packages/integrations/node/test/headers.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture, createRequestAndResponse } from './test-utils.js';
import { expect } from 'chai';

describe('Node Adapter Headers', () => {
/** @type {import('./test-utils').Fixture} */
Expand Down Expand Up @@ -143,5 +144,5 @@ async function runTest(url, expectedHeaders) {
await done;
const headers = res.getHeaders();

expect(headers).to.deep.equal(expectedHeaders);
assert.deepEqual(headers, expectedHeaders);
}
7 changes: 4 additions & 3 deletions packages/integrations/node/test/image.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

Expand All @@ -24,12 +25,12 @@ describe.skip('Image endpoint', () => {

it('it returns images', async () => {
const res = await fixture.fetch('/');
expect(res.status).to.equal(200);
assert.equal(res.status, 200);

const resImage = await fixture.fetch(
'/_image?href=/_astro/some_penguin.97ef5f92.png&w=50&f=webp'
);

expect(resImage.status).to.equal(200);
assert.equal(resImage.status, 200);
});
});
11 changes: 6 additions & 5 deletions packages/integrations/node/test/locals.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
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} */
Expand Down Expand Up @@ -28,7 +29,7 @@ describe('API routes', () => {

let html = await text();

expect(html).to.contain('<h1>bar</h1>');
assert.equal(html.includes('<h1>bar</h1>'), true);
});

it('Throws an error when provided non-objects as locals', async () => {
Expand All @@ -41,7 +42,7 @@ describe('API routes', () => {
req.send();

await done;
expect(res).to.deep.include({ statusCode: 500 });
assert.equal(res.statusCode, 500);
});

it('Can use locals added by astro middleware', async () => {
Expand All @@ -56,7 +57,7 @@ describe('API routes', () => {

const html = await text();

expect(html).to.contain('<h1>baz</h1>');
assert.equal(html.includes('<h1>baz</h1>'), true);
});

it('Can access locals in API', async () => {
Expand All @@ -75,6 +76,6 @@ describe('API routes', () => {

let json = JSON.parse(buffer.toString('utf-8'));

expect(json.foo).to.equal('bar');
assert.equal(json.foo, 'bar');
});
});
14 changes: 8 additions & 6 deletions packages/integrations/node/test/node-middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as assert from 'node:assert/strict';
import { describe, it, before, after } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import { loadFixture, waitServerListen } from './test-utils.js';
import * as cheerio from 'cheerio';
import express from 'express';

Expand Down Expand Up @@ -31,6 +32,7 @@ describe('behavior from middleware, standalone', () => {
const { startServer } = await load();
let res = startServer();
server = res.server;
await waitServerListen(server.server);
});

after(async () => {
Expand All @@ -43,13 +45,13 @@ describe('behavior from middleware, standalone', () => {
it('when mode is standalone', async () => {
const res = await fetch(`http://${server.host}:${server.port}/error-page`);

expect(res.status).to.equal(404);
assert.equal(res.status, 404);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
expect(body.text()).to.equal('Page does not exist');
assert.equal(body.text().includes('Page does not exist'), true);
});
});
});
Expand Down Expand Up @@ -82,12 +84,12 @@ describe('behavior from middleware, middleware', () => {
it('when mode is standalone', async () => {
const res = await fetch(`http://localhost:8888/ssr`);

expect(res.status).to.equal(200);
assert.equal(res.status, 200);

const html = await res.text();
const $ = cheerio.load(html);

const body = $('body');
expect(body.text()).to.contain("Here's a random number");
assert.equal(body.text().includes("Here's a random number"), true);
});
});
Loading
Loading