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

Convert from ava tests to jest #314

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": ["es2015"],
"ignore": "test/*",
"presets": ["@babel/env"],
"ignore": [],
"env": {
"development": {
"sourceMaps": "inline"
Expand Down
19 changes: 6 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
"email": "[email protected]",
"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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
134 changes: 62 additions & 72 deletions test/async-request-transform.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import test from 'ava'
import { create } from '../lib/apisauce'
import createServer from './_server'
import getFreePort from './_getFreePort'
Expand All @@ -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 () => {
Expand All @@ -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()
})
Expand All @@ -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()
jamonholmgren marked this conversation as resolved.
Show resolved Hide resolved
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()
jamonholmgren marked this conversation as resolved.
Show resolved Hide resolved
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 => {
Expand All @@ -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 => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -166,67 +160,63 @@ 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' },
})
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' },
})
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()
})
Loading