From 7177b65ce23caa444a2a7ad37b2a7321263b4d79 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Mon, 15 Jan 2024 00:22:46 -0500 Subject: [PATCH] Stop using chai-as-promised. There's no ESM version yet. --- test/util.ts | 59 +++++++++++++++++++++++++++++++++++++-------------- tsconfig.json | 3 ++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/test/util.ts b/test/util.ts index 1ca080c..36a848d 100644 --- a/test/util.ts +++ b/test/util.ts @@ -1,11 +1,8 @@ import { BadRequest, NotFound } from '@curveball/http-errors'; import * as chai from 'chai'; -import * as chaiAsPromised from 'chai-as-promised'; import { doesMatchRoute, getFilePath, getMimeType, getStaticPrefix, validateFile } from '../src/util.js'; -// @ts-expect-error ESM shenanigans -chai.use(chaiAsPromised.default ?? chaiAsPromised); const expect = chai.expect; describe('getStaticPrefix', () => { @@ -59,23 +56,40 @@ describe('validateFile', () => { const staticDir = `${process.cwd()}/test/assets`; it('should validate file', async () => { - await expect(validateFile(`${staticDir}/test.txt`, staticDir)).to.be.fulfilled; - await expect(validateFile(`${staticDir}/nested/test.txt`, staticDir)).to.be.fulfilled; - await expect(validateFile(`${staticDir}/nested/../test.txt`, staticDir)).to.be.fulfilled; + await validateFile(`${staticDir}/test.txt`, staticDir); + await validateFile(`${staticDir}/nested/test.txt`, staticDir); + await validateFile(`${staticDir}/nested/../test.txt`, staticDir); }); it('should not validate file', async () => { - // Directory - await expect(validateFile(`${staticDir}/`, staticDir)).to.be.rejectedWith(BadRequest); - // File doesn't exist - await expect(validateFile(`${staticDir}/test.nope`, staticDir)).to.be.rejectedWith(NotFound); - // Nested directory - await expect(validateFile(`${staticDir}/nested`, staticDir)).to.be.rejectedWith(BadRequest); - // Relative path to directory - await expect(validateFile(`${staticDir}/../`, staticDir)).to.be.rejectedWith(BadRequest); - // Relative path to missing file - await expect(validateFile(`${staticDir}/../util.ts`, staticDir)).to.be.rejectedWith(BadRequest); + + await assertReject( + validateFile(`${staticDir}/`, staticDir), + BadRequest + ); + await assertReject( + // File doesn't exist + validateFile(`${staticDir}/test.nope`, staticDir), + NotFound + ); + await assertReject( + // Nested directory + validateFile(`${staticDir}/nested`, staticDir), + BadRequest + ); + await assertReject( + // Relative path to directory + validateFile(`${staticDir}/../`, staticDir), + BadRequest + ); + await assertReject( + // Relative path to missing file + validateFile(`${staticDir}/../util.ts`, staticDir), + BadRequest + ); + }); + }); describe('getMimeType', () => { @@ -91,3 +105,16 @@ describe('getMimeType', () => { }); }); + +async function assertReject(promise: Promise, expectedException: any) { + + try { + await promise; + throw new Error('This promise should have rejected, but it didn\'t'); + } catch (err) { + + expect(err).to.be.an.instanceof(expectedException); + + } + +} diff --git a/tsconfig.json b/tsconfig.json index a00d825..63c2b99 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,7 @@ "declaration": true }, "include": [ - "src/**/*" + "src/**/*", + "test/**/*", ] }