Skip to content

Commit

Permalink
@uppy/companion: migrate from Jest to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Aug 15, 2022
1 parent 17db1f6 commit 1fbf974
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 121 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ module.exports = {
},
{
files: ['./packages/@uppy/companion/**/*.js'],
env: {
jest: false,
},
rules: {
'no-underscore-dangle': 'off',

Expand Down
9 changes: 5 additions & 4 deletions packages/@uppy/companion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@
"@types/request": "2.48.4",
"@types/webpack": "^5.28.0",
"@types/ws": "6.0.4",
"into-stream": "^6.0.0",
"jest": "^27.0.6",
"expect": "^28.1.3",
"jest-mock": "^28.1.3",
"nock": "^13.1.3",
"supertest": "3.4.2",
"supertest": "^6.2.0",
"test": "^3.2.1",
"typescript": "~4.4"
},
"files": [
Expand All @@ -107,7 +108,7 @@
"deploy": "kubectl apply -f infra/kube/companion-kube.yml",
"prepublishOnly": "yarn run build",
"start": "node ./lib/standalone/start-server.js",
"test": "jest"
"test": "node--test"
},
"engines": {
"node": "^14.19.0 || ^16.15.0 || >=18.0.0"
Expand Down
27 changes: 23 additions & 4 deletions packages/@uppy/companion/test/__tests__/callback.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
/* global jest:false, test:false, expect:false, describe:false */

const { describe, test } = require('test')
const expect = require('expect').default
const mockOauthState = require('../mockoauthstate')()

const request = require('supertest')
const tokenService = require('../../src/server/helpers/jwt')
const { getServer } = require('../mockserver')

jest.mock('../../src/server/helpers/oauth-state', () => ({
...jest.requireActual('../../src/server/helpers/oauth-state'),
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
mock('../../src/server/helpers/oauth-state', (actual) => ({
...actual,
...mockOauthState,
}))

Expand Down
34 changes: 27 additions & 7 deletions packages/@uppy/companion/test/__tests__/companion.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
/* global jest:false, test:false, expect:false, describe:false */
const { it, describe, it:test, after:afterAll } = require('test')
const expect = require('expect').default

const request = require('supertest')
const nock = require('nock')
const mockOauthState = require('../mockoauthstate')()
const { version } = require('../../package.json')

jest.mock('tus-js-client')
jest.mock('purest')
jest.mock('../../src/server/helpers/oauth-state', () => ({
...jest.requireActual('../../src/server/helpers/oauth-state'),
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
mock('tus-js-client')
mock('purest')
mock('../../src/server/helpers/oauth-state', (actual) => ({
...actual,
...mockOauthState,
}))

const nock = require('nock')
const request = require('supertest')
const tokenService = require('../../src/server/helpers/jwt')
const { getServer } = require('../mockserver')

Expand Down
10 changes: 5 additions & 5 deletions packages/@uppy/companion/test/__tests__/cors.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* global jest:false, test:false, describe:false, expect:false */

const { test, describe } = require('test')
const expect = require('expect').default
const { cors } = require('../../src/server/middlewares')

function testWithMock ({ corsOptions, get = () => {}, origin = 'https://localhost:1234' } = {}) {
const res = {
get,
getHeader: get,
setHeader: jest.fn(),
end: jest.fn(),
setHeader: Function.prototype,
end: Function.prototype,
}
const req = {
method: 'OPTIONS',
headers: {
origin,
},
}
const next = jest.fn()
const next = Function.prototype
cors(corsOptions)(req, res, next)
return { res }
}
Expand Down
24 changes: 22 additions & 2 deletions packages/@uppy/companion/test/__tests__/credentials.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
/* global jest:false, test:false, expect:false, describe:false */
const { test, describe } = require('test')
const expect = require('expect').default

function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
// eslint-disable-next-line import/no-dynamic-require,global-require
const actual = require(package)
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(actual),
},
resetFn: { __proto__: null, value: replacer.bind(null, actual) },
})
}
}
// mocking request module used to fetch custom oauth credentials
jest.mock('request', () => {
mock('request', () => {
const { remoteZoomKey, remoteZoomSecret, remoteZoomVerificationToken } = require('../fixtures/zoom').expects

return {
Expand Down
3 changes: 1 addition & 2 deletions packages/@uppy/companion/test/__tests__/deauthorization.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global test:false, describe:false */

const { test, describe } = require('test')
const request = require('supertest')
const { getServer } = require('../mockserver')

Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion/test/__tests__/header-blacklist.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global test:false, expect:false, describe:false, */

const { test, describe } = require('test')
const expect = require('expect').default
const headerSanitize = require('../../src/server/header-blacklist')

describe('Header black-list testing', () => {
Expand Down
30 changes: 12 additions & 18 deletions packages/@uppy/companion/test/__tests__/http-agent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global test:false, expect:false, describe:false, */

const { test, describe } = require('test')
const expect = require('expect').default
const request = require('request')
const http = require('node:http')
const https = require('node:https')
Expand All @@ -20,49 +20,43 @@ describe('test getRedirectEvaluator', () => {
},
}

test('when original URL has "https:" as protocol', (done) => {
test('when original URL has "https:" as protocol', () => {
const shouldRedirectHttps = getRedirectEvaluator(httpsURL, true)
expect(shouldRedirectHttps(httpsRedirectResp)).toEqual(true)
expect(shouldRedirectHttps(httpRedirectResp)).toEqual(false)
done()
})

test('when original URL has "http:" as protocol', (done) => {
test('when original URL has "http:" as protocol', () => {
const shouldRedirectHttp = getRedirectEvaluator(httpURL, true)
expect(shouldRedirectHttp(httpRedirectResp)).toEqual(true)
expect(shouldRedirectHttp(httpsRedirectResp)).toEqual(false)
done()
})
})

describe('test getProtectedHttpAgent', () => {
test('setting "https:" as protocol', (done) => {
test('setting "https:" as protocol', () => {
const Agent = getProtectedHttpAgent('https:')
expect(Agent).toEqual(https.Agent)
done()
})

test('setting "https" as protocol', (done) => {
test('setting "https" as protocol', () => {
const Agent = getProtectedHttpAgent('https')
expect(Agent).toEqual(https.Agent)
done()
})

test('setting "http:" as protocol', (done) => {
test('setting "http:" as protocol', () => {
const Agent = getProtectedHttpAgent('http:')
expect(Agent).toEqual(http.Agent)
done()
})

test('setting "http" as protocol', (done) => {
test('setting "http" as protocol', () => {
const Agent = getProtectedHttpAgent('http')
expect(Agent).toEqual(http.Agent)
done()
})
})

describe('test protected request Agent', () => {
test('allows URLs without IP addresses', (done) => {
test('allows URLs without IP addresses', (context, done) => {
const options = {
uri: 'https://transloadit.com',
method: 'GET',
Expand All @@ -80,7 +74,7 @@ describe('test protected request Agent', () => {
})
})

test('blocks private http IP address', (done) => {
test('blocks private http IP address', (context, done) => {
const options = {
uri: 'http://172.20.10.4:8090',
method: 'GET',
Expand All @@ -94,7 +88,7 @@ describe('test protected request Agent', () => {
})
})

test('blocks private https IP address', (done) => {
test('blocks private https IP address', (context, done) => {
const options = {
uri: 'https://172.20.10.4:8090',
method: 'GET',
Expand All @@ -108,7 +102,7 @@ describe('test protected request Agent', () => {
})
})

test('blocks localhost IP address', (done) => {
test('blocks localhost IP address', (context, done) => {
const options = {
uri: 'http://127.0.0.1:8090',
method: 'GET',
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/test/__tests__/logger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* global test:false, expect:false, describe:false, beforeAll:false, */
const { it: test, describe, before: beforeAll } = require('test')
const expect = require('expect').default
const chalk = require('chalk')
const logger = require('../../src/server/logger')

Expand Down
23 changes: 20 additions & 3 deletions packages/@uppy/companion/test/__tests__/preauth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
/* global jest:false, test:false, expect:false, describe:false */

jest.mock('../../src/server/helpers/jwt', () => {
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(),
},
resetFn: { __proto__: null, value: replacer },
})
}
}
mock('../../src/server/helpers/jwt', () => {
return {
generateToken: () => {},
verifyToken: () => {},
Expand All @@ -15,6 +30,8 @@ jest.mock('../../src/server/helpers/jwt', () => {
}
})

const { test, describe } = require('test')
const expect = require('expect').default
const request = require('supertest')
const { getServer } = require('../mockserver')
// the order in which getServer is called matters because, once an env is passed,
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion/test/__tests__/provider-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global jest:false, test:false, expect:false, describe:false, beforeEach:false */

const { beforeEach, describe, it:test } = require('test')
const expect = require('expect').default
const providerManager = require('../../src/server/provider')
const { getCompanionOptions } = require('../../src/standalone/helper')
const { setDefaultEnv } = require('../mockserver')
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Test Custom Provider options', () => {
key: 'foo_key',
secret: 'foo_secret',
},
module: jest.mock(),
module: {},
},
}, providers, grantConfig)

Expand Down
39 changes: 33 additions & 6 deletions packages/@uppy/companion/test/__tests__/providers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
/* global jest:false, test:false, expect:false, describe:false */

jest.mock('tus-js-client')
jest.mock('purest')
jest.mock('../../src/server/helpers/request', () => {
function mock (package, replacer) {
const actualPath = require.resolve(package)
if (arguments.length === 1) {
// eslint-disable-next-line import/no-dynamic-require,global-require
require.cache[actualPath] = require(`../__mocks__/${package}`)
} else {
const Module = require('node:module') // eslint-disable-line global-require
require.cache[actualPath] = new Module(actualPath, module)
Object.defineProperties(require.cache[actualPath], {
exports: {
__proto__: null,
value: replacer(),
},
resetFn: { __proto__: null, value: replacer },
})
}
}
mock('tus-js-client')
mock('purest')
mock('../../src/server/helpers/request', () => {
return {
getURLMeta: () => Promise.resolve({ size: 758051 }),
}
})
jest.mock('../../src/server/helpers/oauth-state', () => require('../mockoauthstate')())
mock('../../src/server/helpers/oauth-state', () => require('../mockoauthstate')())

const { before: beforeAll, after: afterAll, describe, it } = require('test')
const expect = require('expect').default
const request = require('supertest')
const nock = require('nock')

const test = {
each (iterable) {
return (message, fn) => {
for (const val of iterable) {
it(message, fn.bind(null, val))
}
}
},
}

const fixtures = require('../fixtures')
const tokenService = require('../../src/server/helpers/jwt')
const { getServer } = require('../mockserver')
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/companion/test/__tests__/subpath.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { it, test } = require('test')
const request = require('supertest')
const { getServer } = require('../mockserver')

Expand Down
Loading

0 comments on commit 1fbf974

Please sign in to comment.