From cc83a8660daf1bbdf079a2b444f1c47d4c4a10e3 Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Tue, 4 Jul 2023 11:45:30 -0700 Subject: [PATCH 1/4] Convert from ava tests to jest --- .babelrc | 4 +- package.json | 19 ++-- test/async-request-transform.test.js | 134 ++++++++++++-------------- test/async-response-transform.test.js | 41 ++++---- test/async.test.js | 12 +-- test/cancellation.test.js | 14 +-- test/config.test.js | 28 +++--- test/data.test.js | 33 +++---- test/headers.test.js | 16 +-- test/monitor.test.js | 42 ++++---- test/no-server.test.js | 10 +- test/params.test.js | 114 ++++++++++------------ test/post-data.test.js | 33 +++---- test/request-transform.test.js | 106 ++++++++++---------- test/response-transform.test.js | 41 ++++---- test/set-base-url.test.js | 16 +-- test/speed.test.js | 16 +-- test/status.test.js | 24 ++--- test/timeout.test.js | 15 ++- test/verbs.test.js | 96 +++++++++--------- 20 files changed, 377 insertions(+), 437 deletions(-) diff --git a/.babelrc b/.babelrc index 4cce85c..0c670ab 100644 --- a/.babelrc +++ b/.babelrc @@ -1,6 +1,6 @@ { - "presets": ["es2015"], - "ignore": "test/*", + "presets": ["@babel/env"], + "ignore": [], "env": { "development": { "sourceMaps": "inline" diff --git a/package.json b/package.json index 4486a58..1e4483b 100644 --- a/package.json +++ b/package.json @@ -5,24 +5,18 @@ "email": "npm@infinite.red", "url": "https://github.com/infinitered/ignite" }, - "ava": { - "require": [ - "babel-core/register" - ] - }, "dependencies": { "axios": "^1.4.0" }, "description": "Axios + standardized errors + request/response transforms.", "devDependencies": { + "@babel/preset-env": "^7.22.6", "@semantic-release/git": "^7.0.5", - "@types/node": "14.0.4", - "ava": "0.25.0", + "@types/node": "20.3.3", "babel-cli": "^6.26.0", - "babel-core": "^6.26.3", - "babel-eslint": "^8.2.3", - "babel-preset-es2015": "^6.24.1", + "babel-eslint": "^10.1.0", "husky": "^1.3.1", + "jest": "^29.6.0", "lint-staged": "^8.1.0", "np": "3.0.4", "npm-run-all": "^4.1.5", @@ -38,7 +32,7 @@ "tslint": "^6.1.3", "tslint-config-prettier": "^1.17.0", "tslint-config-standard": "^9.0.0", - "typescript": "5.1.3" + "typescript": "5.1.6" }, "files": [ "dist/apisauce.js", @@ -62,12 +56,11 @@ "build": "BABEL_ENV=production rollup -c", "clean": "rm -rf dist", "compile": "tsc -p tsconfig.json", - "coverage": "nyc ava", "prepare": "npm-run-all compile build", "dist": "npm-run-all clean compile build test", "lint": "tslint -p .", "test": "npm-run-all compile test:unit", - "test:unit": "ava -s", + "test:unit": "jest", "ci:publish": "yarn semantic-release", "semantic-release": "semantic-release", "format": "prettier --write \"{**/*.ts,.circleci/**/*.js}\" --loglevel error && tslint -p . --fix", diff --git a/test/async-request-transform.test.js b/test/async-request-transform.test.js index b73fe09..dcd4a29 100644 --- a/test/async-request-transform.test.js +++ b/test/async-request-transform.test.js @@ -1,4 +1,3 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' @@ -18,46 +17,45 @@ const delay = time => setTimeout(resolve, time) }) -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('attaches an async request transform', t => { +test('attaches an async request transform', () => { const api = create({ baseURL: `http://localhost:${port}` }) - t.truthy(api.addAsyncRequestTransform) - t.truthy(api.asyncRequestTransforms) - t.is(api.asyncRequestTransforms.length, 0) + expect(api.addAsyncRequestTransform).toBeTruthy() + expect(api.asyncRequestTransforms).toBeTruthy() + expect(api.asyncRequestTransforms.length).toBe(0) api.addAsyncRequestTransform(request => request) - t.is(api.asyncRequestTransforms.length, 1) + expect(api.asyncRequestTransforms.length).toBe(1) }) -test('alters the request data', t => { +test('alters the request data', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.is(count, 0) + expect(count).toBe(0) count = 1 - t.is(req.data.b, 1) + expect(req.data.b).toBe(1) req.data.b = 2 resolve(req) }) }) }) - return x.post('/post', MOCK).then(response => { - t.is(response.status, 200) - t.is(count, 1) - t.is(response.data.got.b, 2) - }) + const response = await x.post('/post', MOCK) + expect(response.status).toBe(200) + expect(count).toBe(1) + expect(response.data.got.b).toBe(2) }) -test('serial async', async t => { +test('serial async', async () => { const api = create({ baseURL: `http://localhost:${port}` }) let fired = false api.addAsyncRequestTransform(request => async () => { @@ -66,20 +64,20 @@ test('serial async', async t => { fired = true }) const response = await api.get('/number/200') - t.true(response.ok) - t.is(response.status, 201) - t.true(fired) + expect(response.ok).toBeTruthy() + expect(response.status).toBe(201) + expect(fired).toBeTruthy() }) -test('transformers should run serially', t => { +test('transformers should run serially', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let first = false let second = false x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.is(second, false) - t.is(first, false) + expect(second).toBeFalsy() + expect(first).toBeFalsy() first = true resolve() }) @@ -88,21 +86,20 @@ test('transformers should run serially', t => { x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.is(first, true) - t.is(second, false) + expect(first).toBeTruthy() + expect(second).toBeFalsy() second = true resolve() }) }) }) - return x.post('/post', MOCK).then(response => { - t.is(response.status, 200) - t.is(first, true) - t.is(second, true) - }) + const response = await x.post('/post', MOCK) + expect(response.status).toBe(200) + expect(first).toBeTruthy() + expect(second).toBeTruthy() }) -test('survives empty PUTs', t => { +test('survives empty PUTs', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addAsyncRequestTransform(req => { @@ -113,14 +110,13 @@ test('survives empty PUTs', t => { }) }) }) - t.is(count, 0) - return x.put('/post', {}).then(response => { - t.is(response.status, 200) - t.is(count, 1) - }) + expect(count).toBe(0) + const response = await x.put('/post', {}) + expect(response.status).toBe(200) + expect(count).toBe(1) }) -test('fires for gets', t => { +test('fires for gets', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addAsyncRequestTransform(req => { @@ -131,15 +127,14 @@ test('fires for gets', t => { }) }) }) - t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(count, 1) - t.deepEqual(response.data, MOCK) - }) + expect(count).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(count).toBe(1) + expect(response.data).toEqual(MOCK) }) -test('url can be changed', t => { +test('url can be changed', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { @@ -149,12 +144,11 @@ test('url can be changed', t => { }) }) }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 200) - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(200) }) -test('params can be added, edited, and deleted', t => { +test('params can be added, edited, and deleted', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { @@ -166,32 +160,30 @@ test('params can be added, edited, and deleted', t => { }) }) }) - return x.get('/number/200', { x: 1, z: 4 }).then(response => { - t.is(response.status, 200) - t.is(response.config.params.x, 2) - t.is(response.config.params.y, 1) - t.falsy(response.config.params.z) - }) + const response = await x.get('/number/200', { x: 1, z: 4 }) + expect(response.status).toBe(200) + expect(response.config.params.x).toBe(2) + expect(response.config.params.y).toBe(1) + expect(response.config.params.z).toBeFalsy() }) -test('headers can be created', t => { +test('headers can be created', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.falsy(req.headers['X-APISAUCE']) + expect(req.headers['X-APISAUCE']).toBeFalsy() req.headers['X-APISAUCE'] = 'new' resolve() }) }) }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.is(response.config.headers['X-APISAUCE'], 'new') - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBe('new') }) -test('headers from creation time can be changed', t => { +test('headers from creation time can be changed', async () => { const x = create({ baseURL: `http://localhost:${port}`, headers: { 'X-APISAUCE': 'hello' }, @@ -199,19 +191,18 @@ test('headers from creation time can be changed', t => { x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.is(req.headers['X-APISAUCE'], 'hello') + expect(req.headers['X-APISAUCE']).toBe('hello') req.headers['X-APISAUCE'] = 'change' resolve() }) }) }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.is(response.config.headers['X-APISAUCE'], 'change') - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBe('change') }) -test('headers can be deleted', t => { +test('headers can be deleted', async () => { const x = create({ baseURL: `http://localhost:${port}`, headers: { 'X-APISAUCE': 'omg' }, @@ -219,14 +210,13 @@ test('headers can be deleted', t => { x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - t.is(req.headers['X-APISAUCE'], 'omg') + expect(req.headers['X-APISAUCE']).toBe('omg') delete req.headers['X-APISAUCE'] resolve() }) }) }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.falsy(response.config.headers['X-APISAUCE']) - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBeFalsy() }) diff --git a/test/async-response-transform.test.js b/test/async-response-transform.test.js index 7b385e8..901934b 100644 --- a/test/async-response-transform.test.js +++ b/test/async-response-transform.test.js @@ -1,32 +1,32 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('attaches a async response transform', t => { +test('attaches a async response transform', () => { const api = create({ baseURL: `http://localhost:${port}` }) console.log(api.asyncResponseTransforms) - t.truthy(api.addAsyncResponseTransform) - t.truthy(api.asyncResponseTransforms) - t.is(api.asyncResponseTransforms.length, 0) + expect(api.addAsyncResponseTransform).toBeTruthy() + expect(api.asyncResponseTransforms).toBeTruthy() + expect(api.asyncResponseTransforms.length).toBe(0) api.addAsyncResponseTransform(data => data) - t.is(api.asyncResponseTransforms.length, 1) + expect(api.asyncResponseTransforms.length).toBe(1) }) -test('alters the response', t => { +test('alters the response', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addAsyncResponseTransform(({ data }) => { @@ -38,15 +38,14 @@ test('alters the response', t => { }) }) }) - t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(count, 1) - t.deepEqual(response.data.a, 'hi') - }) + expect(count).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(count).toBe(1) + expect(response.data.a).toEqual('hi') }) -test('swap out data on response', t => { +test('swap out data on response', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addAsyncResponseTransform(response => { @@ -59,10 +58,8 @@ test('swap out data on response', t => { }) }) }) - // t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 222) - t.is(count, 1) - t.deepEqual(response.data.a, [3, 2, 1]) - }) + const response = await x.get('/number/201') + expect(response.status).toBe(222) + expect(count).toBe(1) + expect(response.data.a).toEqual([3, 2, 1]) }) diff --git a/test/async.test.js b/test/async.test.js index 8e8b380..8a1ae05 100644 --- a/test/async.test.js +++ b/test/async.test.js @@ -1,23 +1,23 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null const MOCK = { a: { b: [1, 2, 3] } } -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after.always('cleanup', t => { +afterAll(() => { server.close() }) -test('can be used with async/await', async t => { +test('can be used with async/await', async () => { const x = create({ baseURL: `http://localhost:${port}` }) const response = await x.get('/number/200', { a: 'b' }) - t.is(response.status, 200) - t.deepEqual(response.data, MOCK) + expect(response.status).toBe(200) + expect(response.data).toEqual(MOCK) }) diff --git a/test/cancellation.test.js b/test/cancellation.test.js index a7907f2..937d503 100644 --- a/test/cancellation.test.js +++ b/test/cancellation.test.js @@ -1,20 +1,20 @@ -import test from 'ava' +import { beforeAll, afterAll, test } from '@jest/globals' import { CancelToken, isCancel, create, CANCEL_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('cancel request', t => { +test('cancel request', () => { const source = CancelToken.source() const x = create({ baseURL: `http://localhost:${port}`, @@ -26,8 +26,8 @@ test('cancel request', t => { }, 20) return x.get('/sleep/150').then(response => { - t.truthy(isCancel(response.originalError)) - t.falsy(response.ok) - t.is(response.problem, CANCEL_ERROR) + expect(isCancel(response.originalError)).toBeTruthy() + expect(response.ok).toBeFalsy() + expect(response.problem).toBe(CANCEL_ERROR) }) }) diff --git a/test/config.test.js b/test/config.test.js index 6968914..06bc363 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -1,35 +1,35 @@ -import test from 'ava' import { create, DEFAULT_HEADERS } from '../lib/apisauce' import axios from 'axios' +import { expect } from '@jest/globals' const validConfig = { baseURL: 'http://localhost:9991', headers: { 'X-Testing': 'hello' }, } -test('is a function', t => { - t.is(typeof create, 'function') +test('is a function', () => { + expect(typeof create).toBe('function') }) -test('returns an object when we configure correctly', t => { +test('returns an object when we configure correctly', () => { const x = create(validConfig) - t.truthy(x) - t.truthy(x.axiosInstance) + expect(x).toBeTruthy() + expect(x.axiosInstance).toBeTruthy() }) -test('configures axios correctly', t => { +test('configures axios correctly', () => { const apisauce = create(validConfig) const { axiosInstance } = apisauce - t.is(axiosInstance.defaults.timeout, 0) - t.is(axiosInstance.defaults.baseURL, validConfig.baseURL) - t.deepEqual(apisauce.headers, Object.assign({}, DEFAULT_HEADERS, validConfig.headers)) + expect(axiosInstance.defaults.timeout).toBe(0) + expect(axiosInstance.defaults.baseURL).toBe(validConfig.baseURL) + expect(apisauce.headers).toEqual(Object.assign({}, DEFAULT_HEADERS, validConfig.headers)) }) -test('configures axios correctly with passed instance', t => { +test('configures axios correctly with passed instance', () => { const customAxiosInstance = axios.create({ baseURL: validConfig.baseURL }) const apisauce = create({ axiosInstance: customAxiosInstance, headers: validConfig.headers }) const { axiosInstance } = apisauce - t.is(axiosInstance.defaults.timeout, 0) - t.is(axiosInstance.defaults.baseURL, validConfig.baseURL) - t.deepEqual(apisauce.headers, Object.assign({}, DEFAULT_HEADERS, validConfig.headers)) + expect(axiosInstance.defaults.timeout).toBe(0) + expect(axiosInstance.defaults.baseURL).toBe(validConfig.baseURL) + expect(apisauce.headers).toEqual(Object.assign({}, DEFAULT_HEADERS, validConfig.headers)) }) diff --git a/test/data.test.js b/test/data.test.js index acbbbbd..46b602e 100644 --- a/test/data.test.js +++ b/test/data.test.js @@ -1,40 +1,37 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after.always('cleanup', t => { +afterAll(() => { server.close() }) -test('has valid data with a 200', t => { +test('has valid data with a 200', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/number/200', { a: 'b' }).then(response => { - t.is(response.status, 200) - t.deepEqual(response.data, MOCK) - }) + const response = await x.get('/number/200', { a: 'b' }) + expect(response.status).toBe(200) + expect(response.data).toEqual(MOCK) }) -test('has valid data with a 400s', t => { +test('has valid data with a 400s', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/number/404').then(response => { - t.is(response.status, 404) - t.deepEqual(response.data, MOCK) - }) + const response = await x.get('/number/404') + expect(response.status).toBe(404) + expect(response.data).toEqual(MOCK) }) -test('has valid data with a 500s', t => { +test('has valid data with a 500s', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/number/500').then(response => { - t.is(response.status, 500) - t.deepEqual(response.data, MOCK) - }) + const response = await x.get('/number/500') + expect(response.status).toBe(500) + expect(response.data).toEqual(MOCK) }) diff --git a/test/headers.test.js b/test/headers.test.js index cf05bef..594b820 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -1,37 +1,37 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('jumps the wire with the right headers', async t => { +test('jumps the wire with the right headers', async () => { const api = create({ baseURL: `http://localhost:${port}`, headers: { 'X-Testing': 'hello' }, }) api.setHeaders({ 'X-Testing': 'foo', steve: 'hey' }) const response = await api.get('/number/200', { a: 'b' }) - t.is(response.config.headers['X-Testing'], 'foo') - t.is(response.config.headers['steve'], 'hey') + expect(response.config.headers['X-Testing']).toBe('foo') + expect(response.config.headers['steve']).toBe('hey') // then change one of them api.setHeader('steve', 'thx') const response2 = await api.get('/number/200', {}) - t.is(response2.config.headers['steve'], 'thx') + expect(response2.config.headers['steve']).toBe('thx') // then remove one of them api.deleteHeader('steve') const response3 = await api.get('/number/200', {}) - t.is(response3.config.headers['steve'], undefined) + expect(response3.config.headers['steve']).toBeUndefined() }) diff --git a/test/monitor.test.js b/test/monitor.test.js index d1c7cba..3128f0b 100644 --- a/test/monitor.test.js +++ b/test/monitor.test.js @@ -1,30 +1,30 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('attaches a monitor', t => { +test('attaches a monitor', () => { const api = create({ baseURL: `http://localhost:${port}` }) - t.truthy(api.addMonitor) - t.truthy(api.monitors) - t.is(api.monitors.length, 0) + expect(api.addMonitor).toBeTruthy() + expect(api.monitors).toBeTruthy() + expect(api.monitors.length).toBe(0) api.addMonitor(x => x) - t.is(api.monitors.length, 1) + expect(api.monitors.length).toBe(1) }) -test('fires our monitor function', t => { +test('fires our monitor function', async () => { let a = 0 let b = 0 const x = create({ baseURL: `http://localhost:${port}` }) @@ -34,15 +34,14 @@ test('fires our monitor function', t => { x.addMonitor(response => { b = response.status }) - t.is(a, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(a, 1) - t.is(b, 201) - }) + expect(a).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(a).toBe(1) + expect(b).toBe(201) }) -test('ignores exceptions raised inside monitors', t => { +test('ignores exceptions raised inside monitors', async () => { let a = 0 let b = 0 const x = create({ baseURL: `http://localhost:${port}` }) @@ -55,10 +54,9 @@ test('ignores exceptions raised inside monitors', t => { x.addMonitor(response => { b = response.status }) - t.is(a, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(a, 1) - t.is(b, 201) - }) + expect(a).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(a).toBe(1) + expect(b).toBe(201) }) diff --git a/test/no-server.test.js b/test/no-server.test.js index ab08ef5..7704363 100644 --- a/test/no-server.test.js +++ b/test/no-server.test.js @@ -1,16 +1,16 @@ -import test from 'ava' import { create, CONNECTION_ERROR } from '../lib/apisauce' import getFreePort from './_getFreePort' +import { beforeAll, test } from '@jest/globals' let port -test.before(async t => { +beforeAll(async () => { port = await getFreePort() }) -test('has a response despite no server', t => { +test('has a response despite no server', () => { const x = create({ baseURL: `http://localhost:${port}` }) return x.get('/number/200', { a: 'b' }).then(response => { - t.is(response.status, null) - t.is(response.problem, CONNECTION_ERROR) + expect(response.status).toBe(null) + expect(response.problem).toBe(CONNECTION_ERROR) }) }) diff --git a/test/params.test.js b/test/params.test.js index 8e2cf9a..b1ff25e 100644 --- a/test/params.test.js +++ b/test/params.test.js @@ -1,111 +1,99 @@ -import test from 'ava' import { create, NONE } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('GET supports params', t => { +test('GET supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo', { q: 'hello' }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.get('/echo', { q: 'hello' }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('POST supports params', t => { +test('POST supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.post('/echo', null, { params: { q: 'hello' } }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.post('/echo', null, { params: { q: 'hello' } }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('PATCH supports params', t => { +test('PATCH supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.patch('/echo', null, { params: { q: 'hello' } }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.patch('/echo', null, { params: { q: 'hello' } }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('PUT supports params', t => { +test('PUT supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.put('/echo', null, { params: { q: 'hello' } }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.put('/echo', null, { params: { q: 'hello' } }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('DELETE supports params', t => { +test('DELETE supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.delete('/echo', { q: 'hello' }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.delete('/echo', { q: 'hello' }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('LINK supports params', t => { +test('LINK supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.link('/echo', { q: 'hello' }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.link('/echo', { q: 'hello' }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('UNLINK supports params', t => { +test('UNLINK supports params', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.unlink('/echo', { q: 'hello' }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: 'hello' }) - }) + const response = await x.unlink('/echo', { q: 'hello' }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: 'hello' }) }) -test('Empty params are supported', t => { +test('Empty params are supported', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo', {}).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: '' }) - }) + const response = await x.get('/echo', {}) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: '' }) }) -test('Null params are supported', t => { +test('Null params are supported', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo', null).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: '' }) - }) + const response = await x.get('/echo', null) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: '' }) }) -test('Undefined params are supported', t => { +test('Undefined params are supported', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo').then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: '' }) - }) + const response = await x.get('/echo') + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: '' }) }) -test('Null parameters should be null', t => { +test('Null parameters should be null', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo', { q: null }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: '' }) - }) + const response = await x.get('/echo', { q: null }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: '' }) }) -test('Empty parameters should be empty', t => { +test('Empty parameters should be empty', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/echo', { q: '' }).then(response => { - t.is(response.problem, NONE) - t.deepEqual(response.data, { echo: '' }) - }) + const response = await x.get('/echo', { q: '' }) + expect(response.problem).toBe(NONE) + expect(response.data).toEqual({ echo: '' }) }) diff --git a/test/post-data.test.js b/test/post-data.test.js index 4086d88..20a6293 100644 --- a/test/post-data.test.js +++ b/test/post-data.test.js @@ -1,40 +1,37 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('POST has proper data', t => { +test('POST has proper data', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.post('/post', MOCK).then(response => { - t.is(response.status, 200) - t.deepEqual(response.data, { got: MOCK }) - }) + const response = await x.post('/post', MOCK) + expect(response.status).toBe(200) + expect(response.data).toEqual({ got: MOCK }) }) -test('PATCH has proper data', t => { +test('PATCH has proper data', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.patch('/post', MOCK).then(response => { - t.is(response.status, 200) - t.deepEqual(response.data, { got: MOCK }) - }) + const response = await x.patch('/post', MOCK) + expect(response.status).toBe(200) + expect(response.data).toEqual({ got: MOCK }) }) -test('PUT has proper data', t => { +test('PUT has proper data', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.put('/post', MOCK).then(response => { - t.is(response.status, 200) - t.deepEqual(response.data, { got: MOCK }) - }) + const response = await x.put('/post', MOCK) + expect(response.status).toBe(200) + expect(response.data).toEqual({ got: MOCK }) }) diff --git a/test/request-transform.test.js b/test/request-transform.test.js index 94ae21b..bd10c3d 100644 --- a/test/request-transform.test.js +++ b/test/request-transform.test.js @@ -1,134 +1,126 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('attaches a request transform', t => { +test('attaches a request transform', () => { const api = create({ baseURL: `http://localhost:${port}` }) - t.truthy(api.addRequestTransform) - t.truthy(api.requestTransforms) - t.is(api.requestTransforms.length, 0) + expect(api.addRequestTransform).toBeTruthy() + expect(api.requestTransforms).toBeTruthy() + expect(api.requestTransforms.length).toBe(0) api.addRequestTransform(request => request) - t.is(api.requestTransforms.length, 1) + expect(api.requestTransforms.length).toBe(1) }) -test('alters the request data', t => { +test('alters the request data', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addRequestTransform(({ data, url, method }) => { data.a = 'hi' count++ }) - t.is(count, 0) - return x.post('/post', MOCK).then(response => { - t.is(response.status, 200) - t.is(count, 1) - t.deepEqual(response.data, { got: { a: 'hi' } }) - }) + expect(count).toBe(0) + const response = await x.post('/post', MOCK) + expect(response.status).toBe(200) + expect(count).toBe(1) + expect(response.data).toEqual({ got: { a: 'hi' } }) }) -test('survives empty PUTs', t => { +test('survives empty PUTs', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addRequestTransform(() => { count++ }) - t.is(count, 0) - return x.put('/post', {}).then(response => { - t.is(response.status, 200) - t.is(count, 1) - }) + expect(count).toBe(0) + const response = await x.put('/post', {}) + expect(response.status).toBe(200) + expect(count).toBe(1) }) -test('fires for gets', t => { +test('fires for gets', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addRequestTransform(({ data, url, method }) => { count++ }) - t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(count, 1) - t.deepEqual(response.data, MOCK) - }) + expect(count).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(count).toBe(1) + expect(response.data).toEqual(MOCK) }) -test('url can be changed', t => { +test('url can be changed', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addRequestTransform(request => { request.url = request.url.replace('/201', '/200') }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 200) - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(200) }) -test('params can be added, edited, and deleted', t => { +test('params can be added, edited, and deleted', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addRequestTransform(request => { request.params.x = 2 request.params.y = 1 delete request.params.z }) - return x.get('/number/200', { x: 1, z: 4 }).then(response => { - t.is(response.status, 200) - t.is(response.config.params.x, 2) - t.is(response.config.params.y, 1) - t.falsy(response.config.params.z) - }) + const response = await x.get('/number/200', { x: 1, z: 4 }) + expect(response.status).toBe(200) + expect(response.config.params.x).toBe(2) + expect(response.config.params.y).toBe(1) + expect(response.config.params.z).toBeFalsy() }) -test('headers can be created', t => { +test('headers can be created', async () => { const x = create({ baseURL: `http://localhost:${port}` }) x.addRequestTransform(request => { - t.falsy(request.headers['X-APISAUCE']) + expect(request.headers['X-APISAUCE']).toBeFalsy() request.headers['X-APISAUCE'] = 'new' }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.is(response.config.headers['X-APISAUCE'], 'new') - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBe('new') }) -test('headers from creation time can be changed', t => { +test('headers from creation time can be changed', async () => { const x = create({ baseURL: `http://localhost:${port}`, headers: { 'X-APISAUCE': 'hello' }, }) x.addRequestTransform(request => { - t.is(request.headers['X-APISAUCE'], 'hello') + expect(request.headers['X-APISAUCE']).toBe('hello') request.headers['X-APISAUCE'] = 'change' }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.is(response.config.headers['X-APISAUCE'], 'change') - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBe('change') }) -test('headers can be deleted', t => { +test('headers can be deleted', async () => { const x = create({ baseURL: `http://localhost:${port}`, headers: { 'X-APISAUCE': 'omg' }, }) x.addRequestTransform(request => { - t.is(request.headers['X-APISAUCE'], 'omg') + expect(request.headers['X-APISAUCE']).toBe('omg') delete request.headers['X-APISAUCE'] }) - return x.get('/number/201', { x: 1 }).then(response => { - t.is(response.status, 201) - t.falsy(response.config.headers['X-APISAUCE']) - }) + const response = await x.get('/number/201', { x: 1 }) + expect(response.status).toBe(201) + expect(response.config.headers['X-APISAUCE']).toBeFalsy() }) diff --git a/test/response-transform.test.js b/test/response-transform.test.js index f9aea1b..c8f3bff 100644 --- a/test/response-transform.test.js +++ b/test/response-transform.test.js @@ -1,45 +1,44 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('attaches a response transform', t => { +test('attaches a response transform', () => { const api = create({ baseURL: `http://localhost:${port}` }) - t.truthy(api.addResponseTransform) - t.truthy(api.responseTransforms) - t.is(api.responseTransforms.length, 0) + expect(api.addResponseTransform).toBeTruthy() + expect(api.responseTransforms).toBeTruthy() + expect(api.responseTransforms.length).toBe(0) api.addResponseTransform(response => response) - t.is(api.responseTransforms.length, 1) + expect(api.responseTransforms.length).toBe(1) }) -test('alters the response', t => { +test('alters the response', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addResponseTransform(({ data }) => { count++ data.a = 'hi' }) - t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(count, 1) - t.deepEqual(response.data.a, 'hi') - }) + expect(count).toBe(0) + const response = await x.get('/number/201') + expect(response.status).toBe(201) + expect(count).toBe(1) + expect(response.data.a).toEqual('hi') }) -test('swap out data on response', t => { +test('swap out data on response', async () => { const x = create({ baseURL: `http://localhost:${port}` }) let count = 0 x.addResponseTransform(response => { @@ -47,10 +46,8 @@ test('swap out data on response', t => { response.status = 222 response.data = { a: response.data.a.b.reverse() } }) - // t.is(count, 0) - return x.get('/number/201').then(response => { - t.is(response.status, 222) - t.is(count, 1) - t.deepEqual(response.data.a, [3, 2, 1]) - }) + const response = await x.get('/number/201') + expect(response.status).toBe(222) + expect(count).toBe(1) + expect(response.data.a).toEqual([3, 2, 1]) }) diff --git a/test/set-base-url.test.js b/test/set-base-url.test.js index 9f15f98..6226eaf 100644 --- a/test/set-base-url.test.js +++ b/test/set-base-url.test.js @@ -1,4 +1,4 @@ -import test from 'ava' +import { beforeAll, afterAll, test } from '@jest/globals' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' @@ -6,29 +6,29 @@ import getFreePort from './_getFreePort' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('changes the headers', async t => { +test('changes the headers', async () => { const api = create({ baseURL: `http://localhost:${port}`, headers: { 'X-Testing': 'hello' }, }) const response1 = await api.get('/number/200') - t.deepEqual(response1.data, MOCK) + expect(response1.data).toEqual(MOCK) // change the url const nextUrl = `http://127.0.0.1:${port}` api.setBaseURL(nextUrl) - t.is(api.getBaseURL(), nextUrl) + expect(api.getBaseURL()).toBe(nextUrl) const response2 = await api.get('/number/200') - t.deepEqual(response2.data, MOCK) + expect(response2.data).toEqual(MOCK) // now close the server server.close() @@ -36,5 +36,5 @@ test('changes the headers', async t => { // and try connecting back to the original one api.setBaseURL(`http://localhost:${port}`) const response3 = await api.get('/number/200') - t.is(response3.problem, 'CONNECTION_ERROR') + expect(response3.problem).toBe('CONNECTION_ERROR') }) diff --git a/test/speed.test.js b/test/speed.test.js index 6ac16a6..05ae6b3 100644 --- a/test/speed.test.js +++ b/test/speed.test.js @@ -1,25 +1,25 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port, MOCK) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('has a duration node', async t => { +test('has a duration node', async () => { const x = create({ baseURL: `http://localhost:${port}` }) const response = await x.get(`/sleep/150`) - t.is(response.status, 200) - t.truthy(response.duration) - t.truthy(response.duration >= 150) - // t.truthy(response.duration <= 1000) // fragile + expect(response.status).toBe(200) + expect(response.duration).toBeTruthy() + expect(response.duration >= 150).toBeTruthy() + // expect(response.duration <= 1000).toBeTruthy() // fragile }) diff --git a/test/status.test.js b/test/status.test.js index 89c4040..29dfbdf 100644 --- a/test/status.test.js +++ b/test/status.test.js @@ -1,39 +1,39 @@ -import test from 'ava' import { create, NONE, CLIENT_ERROR, SERVER_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('reads the status code for 200s', t => { +test('reads the status code for 200s', () => { const x = create({ baseURL: `http://localhost:${port}` }) return x.get('/number/201').then(response => { - t.is(response.status, 201) - t.is(response.problem, NONE) + expect(response.status).toBe(201) + expect(response.problem).toBe(NONE) }) }) -test('reads the status code for 400s', t => { +test('reads the status code for 400s', () => { const x = create({ baseURL: `http://localhost:${port}` }) return x.get('/number/401').then(response => { - t.is(response.status, 401) - t.is(response.problem, CLIENT_ERROR) + expect(response.status).toBe(401) + expect(response.problem).toBe(CLIENT_ERROR) }) }) -test('reads the status code for 500s', t => { +test('reads the status code for 500s', () => { const x = create({ baseURL: `http://localhost:${port}` }) return x.get('/number/501').then(response => { - t.is(response.status, 501) - t.is(response.problem, SERVER_ERROR) + expect(response.status).toBe(501) + expect(response.problem).toBe(SERVER_ERROR) }) }) diff --git a/test/timeout.test.js b/test/timeout.test.js index 8017990..f844554 100644 --- a/test/timeout.test.js +++ b/test/timeout.test.js @@ -1,23 +1,22 @@ -import test from 'ava' import { create, TIMEOUT_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('times out', t => { +test('times out', async () => { const x = create({ baseURL: `http://localhost:${port}`, timeout: 100 }) - return x.get('/sleep/150').then(response => { - t.falsy(response.ok) - t.is(response.problem, TIMEOUT_ERROR) - }) + const response = await x.get('/sleep/150') + expect(response.ok).toBeFalsy() + expect(response.problem).toBe(TIMEOUT_ERROR) }) diff --git a/test/verbs.test.js b/test/verbs.test.js index 7b83751..a8d0ac8 100644 --- a/test/verbs.test.js +++ b/test/verbs.test.js @@ -1,91 +1,83 @@ -import test from 'ava' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' +import { beforeAll, afterAll, expect, test } from '@jest/globals' let port let server = null -test.before(async t => { +beforeAll(async () => { port = await getFreePort() server = await createServer(port) }) -test.after('cleanup', t => { +afterAll(() => { server.close() }) -test('supports all verbs', t => { +test('supports all verbs', () => { const x = create({ baseURL: `http://localhost:${port}` }) - t.truthy(x.get) - t.truthy(x.post) - t.truthy(x.patch) - t.truthy(x.put) - t.truthy(x.head) - t.truthy(x.delete) - t.truthy(x.link) - t.truthy(x.unlink) + expect(x.get).toBeTruthy() + expect(x.post).toBeTruthy() + expect(x.patch).toBeTruthy() + expect(x.put).toBeTruthy() + expect(x.head).toBeTruthy() + expect(x.delete).toBeTruthy() + expect(x.link).toBeTruthy() + expect(x.unlink).toBeTruthy() }) -test('can make a get', t => { +test('can make a get', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.get('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'get') - }) + const response = await x.get('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('get') }) -test('can make a post', t => { +test('can make a post', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.post('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'post') - }) + const response = await x.post('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('post') }) -test('can make a patch', t => { +test('can make a patch', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.patch('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'patch') - }) + const response = await x.patch('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('patch') }) -test('can make a put', t => { +test('can make a put', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.put('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'put') - }) + const response = await x.put('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('put') }) -test('can make a delete', t => { +test('can make a delete', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.delete('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'delete') - }) + const response = await x.delete('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('delete') }) -test('can make a head', t => { +test('can make a head', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.head('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'head') - }) + const response = await x.head('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('head') }) -test('can make a link', t => { +test('can make a link', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.link('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'link') - }) + const response = await x.link('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('link') }) -test('can make a unlink', t => { +test('can make a unlink', async () => { const x = create({ baseURL: `http://localhost:${port}` }) - return x.unlink('/ok').then(response => { - t.truthy(response.ok) - t.is(response.config.method, 'unlink') - }) + const response = await x.unlink('/ok') + expect(response.ok).toBeTruthy() + expect(response.config.method).toBe('unlink') }) From e1334bead1135862fd07803af69f5b7ee93b87bb Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Wed, 19 Jul 2023 21:02:29 -0700 Subject: [PATCH 2/4] Remove global imports --- .gitignore | 1 + test/async-response-transform.test.js | 1 - test/async.test.js | 1 - test/cancellation.test.js | 1 - test/config.test.js | 1 - test/data.test.js | 1 - test/headers.test.js | 1 - test/monitor.test.js | 1 - test/no-server.test.js | 1 - test/params.test.js | 1 - test/post-data.test.js | 1 - test/request-transform.test.js | 1 - test/response-transform.test.js | 1 - test/set-base-url.test.js | 1 - test/speed.test.js | 1 - test/status.test.js | 1 - test/timeout.test.js | 1 - test/verbs.test.js | 1 - 18 files changed, 1 insertion(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 9fbd2f1..6182f06 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ yarn.lock dist .vscode lib/apisauce.js +.config/flame/flame-history.json diff --git a/test/async-response-transform.test.js b/test/async-response-transform.test.js index 901934b..9d8d52c 100644 --- a/test/async-response-transform.test.js +++ b/test/async-response-transform.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/async.test.js b/test/async.test.js index 8a1ae05..dbf73b2 100644 --- a/test/async.test.js +++ b/test/async.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null diff --git a/test/cancellation.test.js b/test/cancellation.test.js index 937d503..762025d 100644 --- a/test/cancellation.test.js +++ b/test/cancellation.test.js @@ -1,4 +1,3 @@ -import { beforeAll, afterAll, test } from '@jest/globals' import { CancelToken, isCancel, create, CANCEL_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' diff --git a/test/config.test.js b/test/config.test.js index 06bc363..a34da1d 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -1,6 +1,5 @@ import { create, DEFAULT_HEADERS } from '../lib/apisauce' import axios from 'axios' -import { expect } from '@jest/globals' const validConfig = { baseURL: 'http://localhost:9991', diff --git a/test/data.test.js b/test/data.test.js index 46b602e..3a46646 100644 --- a/test/data.test.js +++ b/test/data.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/headers.test.js b/test/headers.test.js index 594b820..8ddd1ad 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/monitor.test.js b/test/monitor.test.js index 3128f0b..6d747c9 100644 --- a/test/monitor.test.js +++ b/test/monitor.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/no-server.test.js b/test/no-server.test.js index 7704363..64546ea 100644 --- a/test/no-server.test.js +++ b/test/no-server.test.js @@ -1,6 +1,5 @@ import { create, CONNECTION_ERROR } from '../lib/apisauce' import getFreePort from './_getFreePort' -import { beforeAll, test } from '@jest/globals' let port beforeAll(async () => { diff --git a/test/params.test.js b/test/params.test.js index b1ff25e..3cab33e 100644 --- a/test/params.test.js +++ b/test/params.test.js @@ -1,7 +1,6 @@ import { create, NONE } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null diff --git a/test/post-data.test.js b/test/post-data.test.js index 20a6293..943730e 100644 --- a/test/post-data.test.js +++ b/test/post-data.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/request-transform.test.js b/test/request-transform.test.js index bd10c3d..e0de100 100644 --- a/test/request-transform.test.js +++ b/test/request-transform.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/response-transform.test.js b/test/response-transform.test.js index c8f3bff..5ef60ec 100644 --- a/test/response-transform.test.js +++ b/test/response-transform.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, expect, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/set-base-url.test.js b/test/set-base-url.test.js index 6226eaf..d64b210 100644 --- a/test/set-base-url.test.js +++ b/test/set-base-url.test.js @@ -1,4 +1,3 @@ -import { beforeAll, afterAll, test } from '@jest/globals' import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' diff --git a/test/speed.test.js b/test/speed.test.js index 05ae6b3..1e71318 100644 --- a/test/speed.test.js +++ b/test/speed.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' const MOCK = { a: { b: [1, 2, 3] } } let port diff --git a/test/status.test.js b/test/status.test.js index 29dfbdf..dc43100 100644 --- a/test/status.test.js +++ b/test/status.test.js @@ -1,7 +1,6 @@ import { create, NONE, CLIENT_ERROR, SERVER_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null diff --git a/test/timeout.test.js b/test/timeout.test.js index f844554..48d4c87 100644 --- a/test/timeout.test.js +++ b/test/timeout.test.js @@ -1,7 +1,6 @@ import { create, TIMEOUT_ERROR } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, test } from '@jest/globals' let port let server = null diff --git a/test/verbs.test.js b/test/verbs.test.js index a8d0ac8..f21730c 100644 --- a/test/verbs.test.js +++ b/test/verbs.test.js @@ -1,7 +1,6 @@ import { create } from '../lib/apisauce' import createServer from './_server' import getFreePort from './_getFreePort' -import { beforeAll, afterAll, expect, test } from '@jest/globals' let port let server = null From b1373ae9b0fc4d332bf0086fe9d93a0646f100a3 Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Wed, 19 Jul 2023 21:26:08 -0700 Subject: [PATCH 3/4] Refactor test files for consistency This commit addresses feedback on the test files. It removes explicit imports of globals in all test files for consistency. It also strengthens the assertions in the tests by replacing .toBeTruthy() and .toBeFalsy() with .toBe(true) and .toBe(false) respectively. Additionally, a test in test/cancellation.test.js has been converted to use async/await for better readability and understanding. --- test/async-request-transform.test.js | 14 +++++++------- test/cancellation.test.js | 11 +++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/test/async-request-transform.test.js b/test/async-request-transform.test.js index dcd4a29..93c2b68 100644 --- a/test/async-request-transform.test.js +++ b/test/async-request-transform.test.js @@ -76,8 +76,8 @@ test('transformers should run serially', async () => { x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - expect(second).toBeFalsy() - expect(first).toBeFalsy() + expect(second).toBe(false) + expect(first).toBe(false) first = true resolve() }) @@ -86,8 +86,8 @@ test('transformers should run serially', async () => { x.addAsyncRequestTransform(req => { return new Promise((resolve, reject) => { setImmediate(_ => { - expect(first).toBeTruthy() - expect(second).toBeFalsy() + expect(first).toBe(true) + expect(second).toBe(false) second = true resolve() }) @@ -95,8 +95,8 @@ test('transformers should run serially', async () => { }) const response = await x.post('/post', MOCK) expect(response.status).toBe(200) - expect(first).toBeTruthy() - expect(second).toBeTruthy() + expect(first).toBe(true) + expect(second).toBe(true) }) test('survives empty PUTs', async () => { @@ -218,5 +218,5 @@ test('headers can be deleted', async () => { }) const response = await x.get('/number/201', { x: 1 }) expect(response.status).toBe(201) - expect(response.config.headers['X-APISAUCE']).toBeFalsy() + expect(response.config.headers['X-APISAUCE']).toBe(undefined) }) diff --git a/test/cancellation.test.js b/test/cancellation.test.js index 762025d..7ff8be0 100644 --- a/test/cancellation.test.js +++ b/test/cancellation.test.js @@ -13,7 +13,7 @@ afterAll(() => { server.close() }) -test('cancel request', () => { +test('cancel request', async () => { const source = CancelToken.source() const x = create({ baseURL: `http://localhost:${port}`, @@ -24,9 +24,8 @@ test('cancel request', () => { source.cancel() }, 20) - return x.get('/sleep/150').then(response => { - expect(isCancel(response.originalError)).toBeTruthy() - expect(response.ok).toBeFalsy() - expect(response.problem).toBe(CANCEL_ERROR) - }) + const response = await x.get('/sleep/150') + expect(isCancel(response.originalError)).toBeTruthy() + expect(response.ok).toBeFalsy() + expect(response.problem).toBe(CANCEL_ERROR) }) From f03474338c5062fa3a3e78b06ccd8523bed0c50c Mon Sep 17 00:00:00 2001 From: Jamon Holmgren Date: Wed, 19 Jul 2023 21:30:15 -0700 Subject: [PATCH 4/4] Couple more --- test/async-request-transform.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/async-request-transform.test.js b/test/async-request-transform.test.js index 93c2b68..d72c422 100644 --- a/test/async-request-transform.test.js +++ b/test/async-request-transform.test.js @@ -64,9 +64,9 @@ test('serial async', async () => { fired = true }) const response = await api.get('/number/200') - expect(response.ok).toBeTruthy() + expect(response.ok).toBe(true) expect(response.status).toBe(201) - expect(fired).toBeTruthy() + expect(fired).toBe(true) }) test('transformers should run serially', async () => {