test: add test suite, run in workflow and prepublishOnly

This commit is contained in:
Elliot DeNolf
2023-06-23 10:32:44 -04:00
parent d7adb094a5
commit ed95722a50
7 changed files with 1482 additions and 27 deletions

View File

@@ -1,3 +1,11 @@
module.exports = {
extends: ['@payloadcms'],
overrides: [
{
files: ['**/*.spec.ts'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
],
}

View File

@@ -23,3 +23,4 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: yarn build
- run: yarn test

View File

@@ -0,0 +1,14 @@
module.exports = {
verbose: true,
testEnvironment: 'node',
testMatch: ['**/src/**/*.spec.ts'],
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
},
testTimeout: 60000,
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/src/webpack/mocks/fileMock.js',
'\\.(css|scss)$': '<rootDir>/src/webpack/mocks/emptyModule.js',
},
}

View File

@@ -8,10 +8,11 @@
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "jest",
"lint": "eslint src",
"lint:fix": "eslint --fix --ext .ts,.tsx src",
"clean": "rimraf dist && rimraf dev/yarn.lock",
"prepublishOnly": "yarn clean && yarn build"
"prepublishOnly": "yarn clean && yarn build && yarn test"
},
"keywords": [
"payload",
@@ -38,6 +39,7 @@
"devDependencies": {
"@payloadcms/eslint-config": "^0.0.1",
"@types/express": "^4.17.9",
"@types/jest": "^29.5.2",
"@types/node": "18.11.3",
"@types/react": "18.0.21",
"@typescript-eslint/eslint-plugin": "^5.51.0",
@@ -52,9 +54,11 @@
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"jest": "^29.5.0",
"nodemon": "^2.0.6",
"payload": "^1.10.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.1.3"
}

View File

@@ -0,0 +1,57 @@
import type { Config } from 'payload/config'
import { defaults } from 'payload/dist/config/defaults'
import { sentry } from './plugin'
import * as startSentryFuncs from './startSentry'
let startSentrySpy: jest.SpyInstance
describe('plugin', () => {
beforeEach(() => {
startSentrySpy = jest.spyOn(startSentryFuncs, 'startSentry').mockImplementation(() => {})
})
afterEach(() => {
startSentrySpy.mockRestore()
})
it('should run the plugin', () => {
const plugin = sentry({ enabled: true, dsn: 'asdf' })
const config = plugin(createConfig())
assertPluginRan(config)
})
it('should not run if dsn is not provided', () => {
const plugin = sentry({ enabled: true, dsn: undefined })
const config = plugin(createConfig())
assertPluginDidNotRun(config)
})
it('should respect enabled: false', () => {
const plugin = sentry({ enabled: false })
const config = plugin(createConfig())
assertPluginDidNotRun(config)
})
})
function assertPluginRan(config: Config) {
expect(config.admin?.webpack).toBeDefined()
expect(config.hooks?.afterError).toBeDefined()
expect(startSentrySpy).toHaveBeenCalled()
}
function assertPluginDidNotRun(config: Config) {
expect(config.admin?.webpack).toBeDefined()
expect(config.hooks?.afterError).toBeUndefined()
expect(startSentrySpy).not.toHaveBeenCalled()
}
function createConfig(overrides?: Partial<Config>): Config {
return {
...defaults,
...overrides,
}
}

View File

@@ -23,6 +23,7 @@ export const sentry =
config.hooks = {
...(incomingConfig.hooks || {}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
afterError: (err: any) => {
captureException(err)
},

File diff suppressed because it is too large Load Diff