Skip to content

Commit

Permalink
replace should with built-in node:assert
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot committed Dec 29, 2024
1 parent 4dde92f commit ec421bb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ lint:
./node_modules/.bin/jshint *.js lib bin/* test

test:
node --require should --test
node --test

.PHONY: check lint test
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@
"devDependencies": {
"@pirxpilot/jshint": "^3.0.1",
"connect": ">=2",
"serve-static": "^1.7.0",
"should": "~13"
"serve-static": "^1.7.0"
},
"files": [
"index.js",
"bin",
"lib"
]
}
}
15 changes: 8 additions & 7 deletions test/cachify-static.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');

const cachifyStatic = require('..');

Expand Down Expand Up @@ -37,7 +38,7 @@ test('cachifyStatic custom config', async function (t) {
await t.test('should serve static files', function (t, done) {
const url = helpers.cachify('/a.css');

url.should.be.eql('/B5S3beHW0s-a.css');
assert.equal(url, '/B5S3beHW0s-a.css');

t.request = request(app)
.get(url)
Expand All @@ -47,7 +48,7 @@ test('cachifyStatic custom config', async function (t) {
await t.test('should serve static files from directories', function (t, done) {
const url = helpers.cachify('/texts/b.txt');

url.should.be.eql('/texts/jpmbuwTqzU-b.txt');
assert.equal(url, '/texts/jpmbuwTqzU-b.txt');

t.request = request(app)
.get(url)
Expand All @@ -57,8 +58,8 @@ test('cachifyStatic custom config', async function (t) {
await t.test('should support integrity if needed', function (t, done) {
const url = helpers.cachify('/texts/b.txt', true);

url.should.have.property('path', '/texts/jpmbuwTqzU-b.txt');
url.should.have.property('integrity', 'sha256-1HNeOiZeFu7gP1lxi5tdAwGcB9i2xR+Q2jpmbuwTqzU=');
assert.equal(url.path, '/texts/jpmbuwTqzU-b.txt');
assert.equal(url.integrity, 'sha256-1HNeOiZeFu7gP1lxi5tdAwGcB9i2xR+Q2jpmbuwTqzU=');

t.request = request(app)
.get(url.path)
Expand All @@ -75,7 +76,7 @@ test('cachifyStatic custom config', async function (t) {
t.request = request(app)
.get(helpers.cachify('/a.css'))
.end(function (res) {
res.headers.should.not.have.property('etag');
assert.ok(!('etag' in res.headers));
done();
});
});
Expand Down Expand Up @@ -120,7 +121,7 @@ test('cachifyStatic default config', async function (t) {
await t.test('should serve static files', function (t, done) {
const url = helpers.cachify('/a.css');

url.should.be.eql('/B5S3beHW0s/a.css');
assert.equal(url, '/B5S3beHW0s/a.css');

t.request = request(app)
.get(url)
Expand All @@ -137,7 +138,7 @@ test('cachifyStatic default config', async function (t) {
t.request = request(app)
.get(helpers.cachify('/a.css'))
.end(function (res) {
res.headers.should.have.property('etag');
assert.ok('etag' in res.headers);
done();
});
});
Expand Down
94 changes: 66 additions & 28 deletions test/hash-store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { describe, it, before } = require('node:test');
const should = require('should');
const assert = require('node:assert/strict');
const path = require('path');
const hashStore = require('../lib/hash-store');

Expand All @@ -9,68 +9,106 @@ const hashStore = require('../lib/hash-store');
// d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a66jpmbuwTqzU fixtures/texts/b.txt
// 4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7eZAtRym0n84 fixtures/texts/c.json


describe('hash store', function () {
const root = path.resolve(__dirname, 'fixtures');
let store;

before(async function() {
store = await hashStore(root, /\.json$|\.css$/, function (p, h) {
return h;
});
before(async function () {
store = await hashStore(root, /\.json$|\.css$/, (_, h) => h);
});

it('finds all matching files', function () {
store.getHash('/texts/c.json').should.eql('ZAtRym0n84');
store.getHash('/a.css').should.eql('B5S3beHW0s');
assert.equal(store.getHash('/texts/c.json'), 'ZAtRym0n84');
assert.equal(store.getHash('/a.css'), 'B5S3beHW0s');
});
it('finds all matching files', function () {
assert.equal(store.getHash('/texts/c.json', true).integrity, 'sha256-TgdAhWK+24tgzgXB3s/jrRa3IjCWfeAfZAt+Rym0n84=');
assert.equal(store.getHash('/a.css', true).integrity, 'sha256-a4ayc/80/OGda4BO/1o/V0etpOqiLx1JwB5S3beHW0s=');
});

it('ignores unmatched files', function () {
assert.ok(!store.getHash('/texts/b.txt'));
});

it('ignores nonexistent files', function () {
assert.ok(!store.getHash('/no/such/file'));
});

it('returns paths for valid hashes', function () {
assert.equal(store.getPath('ZAtRym0n84'), '/texts/c.json');
assert.equal(store.getPath('B5S3beHW0s'), '/a.css');
});

it('returns empty for invalid hashes', function () {
assert.equal(store.getPath('89cc14862c'), undefined);
assert.equal(store.getPath('qqqq'), undefined);
assert.equal(store.getPath(), undefined);
});

describe('filter', function () {
it('should match existing files', function () {
const files = store.filter(f => f.endsWith('.css'));
assert.ok(files);
assert.equal(files.length, 1);
assert.equal(files[0], 'B5S3beHW0s');
});

it('should return empty array if nothing found', function () {
const files = store.filter(/\.xyz$/);
assert.ok(files);
assert.equal(files.length, 0);
});

it('** should match all files', function () {
const files = store.filter();
assert.ok(files);
assert.equal(files.length, 2);
assert.equal(files[0], 'B5S3beHW0s');
assert.equal(files[1], 'ZAtRym0n84');
});
});

it('finds all matching files', function () {
store.getHash('/texts/c.json', true).should.have
.property('integrity', 'sha256-TgdAhWK+24tgzgXB3s/jrRa3IjCWfeAfZAt+Rym0n84=');
store.getHash('/a.css', true).should.have
.property('integrity', 'sha256-a4ayc/80/OGda4BO/1o/V0etpOqiLx1JwB5S3beHW0s=');
assert.equal(store.getHash('/texts/c.json', true).integrity, 'sha256-TgdAhWK+24tgzgXB3s/jrRa3IjCWfeAfZAt+Rym0n84=');
assert.equal(store.getHash('/a.css', true).integrity, 'sha256-a4ayc/80/OGda4BO/1o/V0etpOqiLx1JwB5S3beHW0s=');
});

it('ignores unmatched files', function () {
should.not.exist(store.getHash('/texts/b.txt'));
assert.ok(!store.getHash('/texts/b.txt'));
});

it('ignores nonexistent files', function () {
should.not.exist(store.getHash('/no/such/file'));
assert.ok(!store.getHash('/no/such/file'));
});

it('returns paths for valid hashes', function () {
store.getPath('ZAtRym0n84').should.be.eql('/texts/c.json');
store.getPath('B5S3beHW0s').should.be.eql('/a.css');
assert.equal(store.getPath('ZAtRym0n84'), '/texts/c.json');
assert.equal(store.getPath('B5S3beHW0s'), '/a.css');
});

it('returns empty for invalid hashes', function () {
should.not.exist(store.getPath('89cc14862c'));
should.not.exist(store.getPath('qqqq'));
should.not.exist(store.getPath());
assert.equal(store.getPath('89cc14862c'), undefined);
assert.equal(store.getPath('qqqq'), undefined);
assert.equal(store.getPath(), undefined);
});

describe('filter', function () {
it('should match existing files', function () {
const files = store.filter(f => f.endsWith('.css'));
should.exist(files);
files.should.have.length(1);
files[0].should.be.eql('B5S3beHW0s');
assert.ok(files);
assert.deepEqual(files, ['B5S3beHW0s']);
});

it('should return empty array if nothing found', function () {
const files = store.filter(/\.xyz$/);
should.exist(files);
files.should.have.length(0);
assert.ok(files);
assert.equal(files.length, 0);
});

it('** should match all files', function () {
const files = store.filter();
should.exist(files);
files.should.have.length(2);
files[0].should.be.eql('B5S3beHW0s');
files[1].should.be.eql('ZAtRym0n84');
assert.ok(files);
assert.deepEqual(files.sort(), ['B5S3beHW0s', 'ZAtRym0n84']);
});

});
Expand Down
23 changes: 10 additions & 13 deletions test/support/http.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* Module dependencies.
*/

const EventEmitter = require('events').EventEmitter;
const assert = require('node:assert/strict');
const { EventEmitter } = require('events');

const methods = ['get', 'post', 'put', 'delete', 'head'];
const http = require('http');
Expand Down Expand Up @@ -55,22 +52,22 @@ Request.prototype.request = function (method, path) {
return this;
};

Request.prototype.expect = function (body, fn) {
const args = arguments;
Request.prototype.expect = function (body, ...args) {
const fn = args.pop();
this.end(function (res) {
switch (args.length) {
case 3:
res.headers.should.have.property(body.toLowerCase(), args[1]);
args[2]();
case 1:
const header = res.headers[body.toLowerCase()];
assert.equal(header.toLowerCase(), args[0].toLowerCase());
break;
default:
if ('number' == typeof body) {
res.statusCode.should.equal(body);
assert.equal(res.statusCode, body);
} else {
res.body.should.equal(body);
assert.deepEqual(res.body, body);
}
fn();
}
fn();
});
return this;
};
Expand Down

0 comments on commit ec421bb

Please sign in to comment.