Skip to content

Commit de8f08a

Browse files
chore(tests): small improvements (#259)
* feat(e2e): added e2e tests config and setup * remove unnecessary code * fix socket client tests * better test separation * increase timeout for ci * fix integration tests * improve structure * fix socket tests * chore(tests): reorganize tests and improve setup (#257) * fix yarn dev * remove console.log * improvements * jest.config -> jest.unit.config * fix * fix * fix * remove some unecessary global setups and teardowns Co-authored-by: Laurent Leclerc-Poulin <[email protected]> Co-authored-by: Laurent Leclerc Poulin <[email protected]>
1 parent 9eb89cf commit de8f08a

16 files changed

+49
-66
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
yarn --frozen-lockfile
3636
- name: Run tests
3737
run: |
38-
yarn test:functional --coverage --verbose
38+
yarn test:func --coverage --verbose
3939
4040
e2e:
4141
name: E2E

doc/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You can configure the server using environmnent variables. An `.env` file in the
2020
| REDIS_OPTIONS | Advances options for redis. Refer to the [ioredis documentation](https://github.com/luin/ioredis/blob/master/API.md) (ex: `{"password":"admin123","connectTimeout":20000})` |
2121
| REDIS_SCOPE | Allows to prefix Redis channels and keys when multiple clients use the same Redis cluster (e.g. `development`, `production`, `server-a`) |
2222
| LOGGING_ENABLED | Toggles if incoming and outgoing messages are logged to the console. Defaults to `false` when in `production` mode and `true` when in `development` mode |
23+
| SUPPRESS_LOGGING | Completely disables all logging to the console |
2324
| SYNC | Array of sync requests to be executed when the server starts. This can be used to configure sandboxes, or test clients |
2425
| SKIP_LOAD_ENV | If set to `true`, the server will skip loading any config from the `.env` file |
2526
| NO_LOGO | Toggles the Messaging Server logo when starting the server |

jest.e2e.config.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Config } from '@jest/types'
22
import { defaults as tsjPreset } from 'ts-jest/presets'
33
import { pathsToModuleNameMapper } from 'ts-jest/utils'
4-
5-
const ClientConfig = require('./packages/client/test/tsconfig.json')
6-
const SocketConfig = require('./packages/socket/test/tsconfig.json')
4+
import ClientConfig from './packages/client/test/tsconfig.json'
5+
import SocketConfig from './packages/socket/test/tsconfig.json'
76

87
const config: Config.InitialOptions = {
98
preset: 'ts-jest',

jest.functional.config.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import type { Config } from '@jest/types'
22
import { defaults as tsjPreset } from 'ts-jest/presets'
33
import { pathsToModuleNameMapper } from 'ts-jest/utils'
4-
5-
const ServerConfig = require('./packages/server/test/tsconfig.json')
4+
import ServerConfig from './packages/server/test/tsconfig.json'
65

76
const config: Config.InitialOptions = {
87
preset: 'ts-jest',
9-
globalSetup: './test/jest.functional.setup.ts',
108
globalTeardown: './test/jest.functional.teardown.ts',
119
projects: [
1210
{

jest.config.ts jest.unit.config.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { Config } from '@jest/types'
22
import { defaults as tsjPreset } from 'ts-jest/presets'
33
import { pathsToModuleNameMapper } from 'ts-jest/utils'
4-
5-
const ServerConfig = require('./packages/server/test/tsconfig.json')
4+
import ServerConfig from './packages/server/test/tsconfig.json'
65

76
const config: Config.InitialOptions = {
87
preset: 'ts-jest',
@@ -15,8 +14,6 @@ const config: Config.InitialOptions = {
1514
statements: 60
1615
}
1716
}*/
18-
globalSetup: './test/jest.setup.ts',
19-
globalTeardown: './test/jest.teardown.ts',
2017
projects: [
2118
{
2219
rootDir: 'packages/engine',

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"start": "yarn workspace @botpress/messaging-server start",
1717
"dev": "yarn workspace @botpress/messaging-server dev",
1818
"board": "yarn workspace @botpress/messaging-board dev",
19-
"test": "yarn test:unit && yarn test:functional && yarn test:e2e",
20-
"test:unit": "yarn --silent && jest",
21-
"test:functional": "yarn --silent && jest -c jest.functional.config.ts",
22-
"test:e2e": "yarn --silent && jest -c jest.e2e.config.ts"
19+
"test": "yarn test:unit && yarn test:func && yarn test:e2e",
20+
"test:unit": "jest -c jest.unit.config.ts",
21+
"test:func": "jest -c jest.functional.config.ts",
22+
"test:e2e": "jest -c jest.e2e.config.ts"
2323
},
2424
"bin": "./packages/server/dist/index.js",
2525
"pkg": {

packages/engine/src/global.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface EngineEnv {
33

44
// logger
55
LOGGING_ENABLED?: string
6+
SUPPRESS_LOGGING?: string
67
SINGLE_LINE_LOGGING?: string
78
DISABLE_LOGGING_TIMESTAMP?: string
89

packages/engine/src/logger/types.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ export class Logger {
111111
definedParams = this.singleLine(definedParams)
112112
}
113113

114-
// eslint-disable-next-line no-console
115-
console.log(...definedParams)
114+
if (!yn(process.env.SUPPRESS_LOGGING)) {
115+
// eslint-disable-next-line no-console
116+
console.log(...definedParams)
117+
}
116118
}
117119

118120
private singleLine(params: Param[]) {

packages/server/test/functional/utils/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
import path from 'path'
12
import { v4 as uuidv4 } from 'uuid'
23
import { App } from '../../../src/app'
34

45
let app: App
56

67
const setupApp = async () => {
7-
process.env.DATABASE_URL = `.test-data/${uuidv4()}.sqlite`
8+
process.env.SKIP_LOAD_ENV = 'true'
9+
process.env.SUPPRESS_LOGGING = 'true'
10+
process.env.DATABASE_URL = path.join(
11+
__dirname,
12+
'..',
13+
'..',
14+
'..',
15+
'..',
16+
'..',
17+
'test',
18+
'.test-data',
19+
`${uuidv4()}.sqlite`
20+
)
821

922
app = new App()
1023
await app.setup()

test/jest.e2e.setup.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
require('ts-node/register')
33

44
import { setup as setupDevServer } from 'jest-dev-server'
5-
import jestSetup from './jest.setup'
5+
import path from 'path'
6+
import { v4 as uuidv4 } from 'uuid'
67

78
const setup = async () => {
8-
await jestSetup()
9-
10-
// TODO: Maybe put this somewhere else?
9+
process.env.SKIP_LOAD_ENV = 'true'
1110
process.env.ENABLE_EXPERIMENTAL_SOCKETS = 'true'
11+
process.env.DATABASE_URL = path.join(__dirname, '.test-data', `${uuidv4()}.sqlite`)
1212

1313
await setupDevServer({
1414
command: 'yarn dev',

test/jest.e2e.teardown.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import fs from 'fs'
12
import { teardown as teardownDevServer } from 'jest-dev-server'
2-
const jestTeardown = require('./jest.teardown').default
3+
import path from 'path'
34

45
const teardown = async () => {
5-
await jestTeardown()
6+
const dir = path.join(__dirname, '.test-data')
7+
if (fs.existsSync(dir)) {
8+
fs.rmdirSync(dir, { recursive: true })
9+
}
10+
611
await teardownDevServer()
712
}
813

test/jest.functional.setup.ts

-10
This file was deleted.

test/jest.functional.teardown.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
const jestTeardown = require('./jest.teardown').default
1+
import fs from 'fs'
2+
import path from 'path'
23

34
const teardown = async () => {
4-
await jestTeardown()
5+
const dir = path.join(__dirname, '.test-data')
6+
if (fs.existsSync(dir)) {
7+
fs.rmdirSync(dir, { recursive: true })
8+
}
59
}
610

711
export default teardown

test/jest.setup.ts

-17
This file was deleted.

test/jest.teardown.ts

-12
This file was deleted.

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"include": [],
99
"exclude": ["node_modules", "**/node_modules"],
1010
"compilerOptions": {
11-
"esModuleInterop": true
11+
"esModuleInterop": true,
12+
"resolveJsonModule": true,
13+
"strict": true
1214
}
1315
}

0 commit comments

Comments
 (0)