Skip to content

Commit

Permalink
Switch to vitest (#176)
Browse files Browse the repository at this point in the history
This replaces Jest as a test runner with Vitest. Jest has too many
issues resolving modules and running TypeScript.

The tests were renamed to match Vitest’s default test pattern.

The ESLint configuration was adjusted a bit to match the new setup as
well.
  • Loading branch information
remcohaszing authored Aug 22, 2024
1 parent d336ac2 commit 0868ca5
Show file tree
Hide file tree
Showing 12 changed files with 1,092 additions and 1,762 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
module.exports = {
root: true,
extends: ['transloadit', 'prettier'],
parserOptions: {
ecmaVersion: 11,
requireConfigFile: false,
},
overrides: [
{
files: 'test/**',
globals: {
afterAll: true,
afterEach: true,
beforeAll: true,
beforeEach: true,
describe: true,
it: true,
vi: true,
},
rules: {
'no-console': 'off',
},
},
],
}
5 changes: 0 additions & 5 deletions jsconfig.json

This file was deleted.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
"uuid": "^8.3.2"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/eslint-parser": "^7.15.8",
"@babel/eslint-plugin": "^7.13.10",
"@types/jest": "^26.0.19",
"@vitest/coverage-v8": "^2.0.5",
"badge-maker": "^3.3.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^8.8.0",
Expand All @@ -42,13 +43,13 @@
"eslint-plugin-react": "^7.23.1",
"eslint-plugin-react-hooks": "^4.2.0",
"execa": "5",
"jest": "^29.7.0",
"nock": "^13.0.5",
"npm-run-all": "^4.1.5",
"p-retry": "^4.2.0",
"prettier": "^2.8.6",
"temp": "^0.9.1",
"tsd": "^0.25.0"
"tsd": "^0.25.0",
"vitest": "^2.0.5"
},
"repository": {
"type": "git",
Expand All @@ -63,10 +64,10 @@
"lint": "npm-run-all --parallel 'lint:*'",
"fix": "npm-run-all --serial 'fix:*'",
"next:update": "next-update --keep true --tldr",
"test-unit": "jest --detectOpenHandles --coverage ./test/unit",
"test-integration": "jest --verbose --detectOpenHandles --runInBand ./test/integration",
"test-unit": "vitest run --coverage ./test/unit",
"test-integration": "vitest run ./test/integration",
"tsd": "tsd",
"test-all": "npm run tsd && jest --detectOpenHandles --runInBand --coverage --coverageReporters json lcov text clover json-summary --forceExit",
"test-all": "npm run tsd && vitest run --coverage --coverageReporters json lcov text clover json-summary --forceExit",
"test": "npm run tsd && npm run test-unit",
"fix:formatting": "prettier --write .",
"lint:formatting": "prettier --check ."
Expand Down
4 changes: 2 additions & 2 deletions src/Transloadit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const uuid = require('uuid')
const InconsistentResponseError = require('./InconsistentResponseError')
const PaginationStream = require('./PaginationStream')
const { version } = require('../package.json')
const { sendTusRequest } = require('./tus')
const tus = require('./tus')

function decorateHttpError(err, body) {
if (!body) return err
Expand Down Expand Up @@ -216,7 +216,7 @@ class TransloaditClient {
checkResult(result)

if (isResumable && Object.keys(allStreamsMap).length > 0) {
await sendTusRequest({
await tus.sendTusRequest({
streamsMap: allStreamsMap,
assembly: result,
onProgress: onUploadProgress,
Expand Down
8 changes: 0 additions & 8 deletions test/.eslintrc.js

This file was deleted.

File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
* @jest-environment node
*/
// https://github.com/axios/axios/issues/2654
const keyBy = require('lodash/keyBy')
const querystring = require('querystring')
Expand All @@ -16,9 +13,9 @@ const debug = require('debug')('transloadit:live-api')

const pipeline = promisify(streamPipeline)

const Transloadit = require('../../../src/Transloadit')
const Transloadit = require('../../src/Transloadit')

const { createTestServer } = require('../../testserver')
const { createTestServer } = require('../testserver')

async function downloadTmpFile(url) {
const { path } = await temp.open('transloadit')
Expand Down Expand Up @@ -104,8 +101,6 @@ const genericOptions = {
waitForCompletion: true,
}

jest.setTimeout(100000)

const handlers = new Map()

let testServer
Expand All @@ -130,7 +125,7 @@ beforeAll(async () => {
debug('Invalid path match', req.url)
}
})
})
}, 100000)

afterAll(async () => {
await testServer?.close()
Expand All @@ -152,7 +147,7 @@ async function createVirtualTestServer(handler) {
}
}

describe('API integration', () => {
describe('API integration', { timeout: 30000 }, () => {
describe('assembly creation', () => {
it('should create a retrievable assembly on the server', async () => {
const client = createClient()
Expand Down Expand Up @@ -527,30 +522,32 @@ describe('API integration', () => {
)
})

it('should be able to handle pagination with a stream', (done) => {
it('should be able to handle pagination with a stream', async () => {
const client = createClient()
const assemblies = client.streamAssemblies({ pagesize: 2 })
let n = 0
let isDone = false

assemblies.on('readable', () => {
const assembly = assemblies.read()
await new Promise((resolve) => {
assemblies.on('readable', () => {
const assembly = assemblies.read()

if (isDone) return
if (isDone) return

if (assembly == null) {
done()
return
}
if (assembly == null) {
resolve()
return
}

if (n === 5) {
isDone = true
done()
return
}
if (n === 5) {
isDone = true
resolve()
return
}

expect(assembly).toHaveProperty('id')
n++
expect(assembly).toHaveProperty('id')
n++
})
})
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const nock = require('nock')

const Transloadit = require('../../../src/Transloadit')

jest.setTimeout(1000)
const Transloadit = require('../../src/Transloadit')

const getLocalClient = (opts) =>
new Transloadit({ authKey: '', authSecret: '', endpoint: 'http://localhost', ...opts })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Writable } = require('stream')
const _ = require('lodash')

const PaginationStream = require('../../../src/PaginationStream')
const PaginationStream = require('../../src/PaginationStream')

const toArray = (callback) => {
const stream = new Writable({ objectMode: true })
Expand All @@ -14,7 +14,7 @@ const toArray = (callback) => {
}

describe('PaginationStream', () => {
it('should preserve order with synchronous data sources', (done) => {
it('should preserve order with synchronous data sources', async () => {
const count = 9
const pages = [
{ count, items: [1, 2, 3] },
Expand All @@ -24,22 +24,24 @@ describe('PaginationStream', () => {

const stream = new PaginationStream(async (pageno) => pages[pageno - 1])

stream.pipe(
toArray((array) => {
const expected = _.flatten(
Array.from(pages).map(({ items }) => items),
true
)
await new Promise((resolve) => {
stream.pipe(
toArray((array) => {
const expected = _.flatten(
Array.from(pages).map(({ items }) => items),
true
)

expect(array).toEqual(expected)
done()
})
)
expect(array).toEqual(expected)
resolve()
})
)

stream.resume()
stream.resume()
})
})

it('should preserve order with asynchronous data sources', (done) => {
it('should preserve order with asynchronous data sources', async () => {
const count = 9
const pages = [
{ count, items: [1, 2, 3] },
Expand All @@ -51,18 +53,20 @@ describe('PaginationStream', () => {
async (pageno) => new Promise((resolve) => process.nextTick(() => resolve(pages[pageno - 1])))
)

stream.pipe(
toArray((array) => {
const expected = _.flatten(
Array.from(pages).map(({ items }) => items),
true
)
await new Promise((resolve) => {
stream.pipe(
toArray((array) => {
const expected = _.flatten(
Array.from(pages).map(({ items }) => items),
true
)

expect(array).toEqual(expected)
done()
})
)
expect(array).toEqual(expected)
resolve()
})
)

stream.resume()
stream.resume()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ const { Readable: ReadableStream } = require('stream')
const FormData = require('form-data')
const got = require('got')

jest.mock('../../../src/tus')

const tus = require('../../../src/tus')
const Transloadit = require('../../../src/Transloadit')
const packageVersion = require('../../../package.json').version

jest.mock('got')

tus.sendTusRequest.mockImplementation(() => {})
const tus = require('../../src/tus')
const Transloadit = require('../../src/Transloadit')
const packageVersion = require('../../package.json').version

const mockedExpiresDate = '2021-01-06T21:11:07.883Z'
const mockGetExpiresDate = (client) =>
jest.spyOn(client, '_getExpiresDate').mockReturnValue(mockedExpiresDate)
vi.spyOn(client, '_getExpiresDate').mockReturnValue(mockedExpiresDate)
const mockGot = (method) =>
got[method].mockImplementation(() => {
vi.spyOn(got, method).mockImplementation(() => {
const mockPromise = Promise.resolve({ body: '' })
mockPromise.on = jest.fn(() => {})
mockPromise.on = vi.fn(() => {})
return mockPromise
})
const mockRemoteJson = (client) =>
jest.spyOn(client, '_remoteJson').mockImplementation(() => ({ body: {} }))
vi.spyOn(client, '_remoteJson').mockImplementation(() => ({ body: {} }))

describe('Transloadit', () => {
it('should throw a proper error for request stream', async () => {
Expand Down Expand Up @@ -88,10 +82,11 @@ describe('Transloadit', () => {

describe('add stream', () => {
it('should pause streams', async () => {
vi.spyOn(tus, 'sendTusRequest').mockImplementation(() => {})
const client = new Transloadit({ authKey: 'foo_key', authSecret: 'foo_secret' })

const name = 'foo_name'
const pause = jest.fn(() => {})
const pause = vi.fn(() => {})
const mockStream = {
pause,
pipe: () => {},
Expand Down Expand Up @@ -128,8 +123,8 @@ describe('Transloadit', () => {
}

mockGetExpiresDate(client)
const calcSignatureSpy = jest.spyOn(client, 'calcSignature')
const formAppendSpy = jest.spyOn(form, 'append')
const calcSignatureSpy = vi.spyOn(client, 'calcSignature')
const formAppendSpy = vi.spyOn(form, 'append')

client._appendForm(form, params, streamsMap, fields)

Expand Down Expand Up @@ -214,7 +209,7 @@ describe('Transloadit', () => {

mockGetExpiresDate(client)

const prepareParamsSpy = jest.spyOn(client, '_prepareParams')
const prepareParamsSpy = vi.spyOn(client, '_prepareParams')

const r = client.calcSignature(params)

Expand Down
10 changes: 10 additions & 0 deletions vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
coverage: {
include: 'src',
},
globals: true,
},
})
Loading

0 comments on commit 0868ca5

Please sign in to comment.