Skip to content

Commit

Permalink
Ignore test marked with todo (#257)
Browse files Browse the repository at this point in the history
* πŸ”§ Add eslint prettier integration

Also add editor config

* ♻️ Ignore tests marked with todo

* πŸ’„ Run eslint fix

* πŸ”§ Use mts for vitest config
  • Loading branch information
eratio08 authored Feb 7, 2024
1 parent 8ad8666 commit 8a32a8b
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
[*]
charset = utf-8
insert_final_newline = true
end_of_line = lf
indent_style = space
indent_size = 2
max_line_length = 140
32 changes: 18 additions & 14 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"env": {
"node": true,
"es2021": true
},
"extends": "standard-with-typescript",
"parserOptions": {
"project": "./tsconfig.json",
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/space-before-function-paren": ["error", "never"],
"@typescript-eslint/prefer-nullish-coalescing": "off"
}
"env": {
"node": true,
"es2021": true
},
"extends": "standard-with-typescript",
"parserOptions": {
"project": "./tsconfig.json",
"ecmaVersion": "latest",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/space-before-function-paren": [
"error",
"never"
],
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/comma-dangle": "off"
}
}
5 changes: 2 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"printWidth": 140,
"tabWidth": 2,
"singleQuote": true
"singleQuote": true,
"semi": false
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@vitest/coverage-v8": "^1.0.1",
"eslint": "^8.46.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.0",
"eslint-plugin-import": "^2.28.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"rimraf": "^5.0.1",
"typescript": "^5.1.6",
Expand Down
70 changes: 70 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions src/app/escape.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export const escape = (str: string | number = ''): string => {
return str
.toString()
// eslint-disable-next-line no-control-regex
.replace(/\x1B.*?m/g, '')
.replace(/\|/g, '||')
.replace(/\n/g, '|n')
.replace(/\r/g, '|r')
.replace(/\[/g, '|[')
.replace(/]/g, '|]')
.replace(/\u0085/g, '|x')
.replace(/\u2028/g, '|l')
.replace(/\u2029/g, '|p')
.replace(/'/g, "|'")
return (
str
.toString()
// eslint-disable-next-line no-control-regex
.replace(/\x1B.*?m/g, '')
.replace(/\|/g, '||')
.replace(/\n/g, '|n')
.replace(/\r/g, '|r')
.replace(/\[/g, '|[')
.replace(/]/g, '|]')
.replace(/\u0085/g, '|x')
.replace(/\u2028/g, '|l')
.replace(/\u2029/g, '|p')
.replace(/'/g, "|'")
)
}
10 changes: 4 additions & 6 deletions src/app/messages/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ export type Parameters = { [key in string]: string | number }

// https://www.jetbrains.com/help/teamcity/service-messages.html#Supported+Test+ServiceMessages
export abstract class Message {
public constructor(
protected id: string,
protected name: string
) {
}
public constructor(protected id: string, protected name: string) {}

protected abstract generate(type: string, parameters: Parameters): string

protected generateParameters(parameters: Parameters): string {
return Object.entries(parameters).map(([key, value]) => `${key}='${escape(value ?? '')}'`).join(' ')
return Object.entries(parameters)
.map(([key, value]) => `${key}='${escape(value ?? '')}'`)
.join(' ')
}

protected generateTeamcityMessage(type: string, flowId: string, parameters: Parameters): string {
Expand Down
4 changes: 2 additions & 2 deletions src/app/messages/test-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class TestMessage extends Message {
return this.generate('testFailed', {
message: escape(error?.message ?? ''),
details: escape(error?.stackStr ?? ''),
actual: escape(error?.actual ?? ''),
expected: escape(error?.expected ?? '')
actual: escape(error?.actual as string ?? ''),
expected: escape(error?.expected as string ?? ''),
})
}

Expand Down
52 changes: 23 additions & 29 deletions src/app/printer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { type File, type Suite, type Task, type TaskResultPack, type Test, type UserConsoleLog, type Vitest, type ErrorWithDiff } from 'vitest'
import {
type File,
type Suite,
type Task,
type TaskResultPack,
type Test,
type UserConsoleLog,
type Vitest,
type ErrorWithDiff,
} from 'vitest'
import { SuitMessage } from './messages/suite-message'
import { escape } from './escape'
import { TestMessage } from './messages/test-message'
Expand All @@ -11,16 +20,11 @@ export class Printer {
private readonly fileMessageMap = new Map<string, PotentialMessages>()
private readonly testConsoleMap = new Map<string, UserConsoleLog[]>()

constructor(private readonly logger: Vitest['logger']) {
}
constructor(private readonly logger: Vitest['logger']) {}

public addFile = (file: File): void => {
const suitMessage = new SuitMessage(file.id, escape(file.name))
const messages = [
suitMessage.started(),
...file.tasks.flatMap(this.handleTask),
suitMessage.finished()
]
const messages = [suitMessage.started(), ...file.tasks.flatMap(this.handleTask), suitMessage.finished()]
this.fileMessageMap.set(file.id, messages)
}

Expand All @@ -35,10 +39,12 @@ export class Printer {

public handeUpdate = ([id, result]: TaskResultPack): void => {
const messages = this.fileMessageMap.get(id)
if ((messages != null) && (result != null) && result.state !== 'run') {
if (messages != null && result != null && result.state !== 'run') {
messages
.flatMap((message: PotentialMessage) => typeof message === 'string' ? message : message())
.forEach(message => { this.logger.console.info(message) })
.flatMap((message: PotentialMessage) => (typeof message === 'string' ? message : message()))
.forEach((message) => {
this.logger.console.info(message)
})
this.fileMessageMap.delete(id)
}
}
Expand All @@ -56,37 +62,25 @@ export class Printer {
private readonly handleSuite = (suite: Suite): PotentialMessage[] => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const suitMessage = new SuitMessage(suite.file!.id, escape(suite.name))
return [
suitMessage.started(),
...suite.tasks.flatMap(this.handleTask),
suitMessage.finished()
]
return [suitMessage.started(), ...suite.tasks.flatMap(this.handleTask), suitMessage.finished()]
}

private readonly handleTest = (test: Test): PotentialMessage => {
const testMessage = new TestMessage(test)
if (test.mode === 'skip') {
if (test.mode === 'skip' || test.mode === 'todo') {
return testMessage.ignored()
}
return () => {
const fail = (test.result == null) || test.result.state === 'fail'
const fail = test.result == null || test.result.state === 'fail'

const logs = this.testConsoleMap.get(test.id) ?? []
const logsMessages = logs.map(log => testMessage.log(log.type, log.content))
const logsMessages = logs.map((log) => testMessage.log(log.type, log.content))
const filedMessages = fail ? this.getTestErrors(test).map(testMessage.fail) : []

return [
testMessage.started(),
...logsMessages,
...filedMessages,
testMessage.finished(test.result?.duration ?? 0)
].filter(Boolean)
return [testMessage.started(), ...logsMessages, ...filedMessages, testMessage.finished(test.result?.duration ?? 0)].filter(Boolean)
}
}

private readonly getTestErrors = (test: Test): ErrorWithDiff[] =>
test.result?.errors ??
test.suite.result?.errors ??
test.file?.result?.errors ??
[new MissingResultError(test)]
test.result?.errors ?? test.suite.result?.errors ?? test.file?.result?.errors ?? [new MissingResultError(test)]
}
2 changes: 1 addition & 1 deletion src/app/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TeamCityReporter implements Reporter {
}

onTaskUpdate(packs: TaskResultPack[]): Awaitable<void> {
return new Promise<void>(resolve => {
return new Promise<void>((resolve) => {
setTimeout(() => {
packs.reverse().forEach(this.printer.handeUpdate)
resolve()
Expand Down
3 changes: 2 additions & 1 deletion src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import missTestWithoutProblemExpect from './miss-test-result/miss-test-result-wi
import missTestWithProblemExpect from './miss-test-result/miss-test-result-with-problem.expect'
import sequenceSyncExpect from './sequence-check/sync.expect'
import { compareResultWithExpect, generateExpectTest } from './utils'

describe('main tests', () => {
let consoleStub: any

Expand Down Expand Up @@ -36,7 +37,7 @@ describe('main tests', () => {

expect(consoleStub.info).toHaveBeenCalled()
expect(consoleStub.log).not.toHaveBeenCalled()
expect(info.length).toEqual(12)
expect(info.length).toEqual(13)
compareResultWithExpect(workCheckExpect, info)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export default [
['testFailed', 'second test delays the execution result'],
['testFinished', 'second test delays the execution result'],
['testSuiteFinished', 'With delayed hook'],
['testSuiteFinished', 'src/test/miss-test-result/miss-test-result-with-problem.spec.ts']
['testSuiteFinished', 'src/test/miss-test-result/miss-test-result-with-problem.spec.ts'],
]
2 changes: 1 addition & 1 deletion src/test/sequence-check/async-2.expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export default [
['testFinished', 'should log after 1000 ms'],
['testSuiteFinished', 'Test log with async'],
['testSuiteFinished', 'Example sequence-check async file 2'],
['testSuiteFinished', 'src/test/sequence-check/async-2.spec.ts']
['testSuiteFinished', 'src/test/sequence-check/async-2.spec.ts'],
]
Loading

0 comments on commit 8a32a8b

Please sign in to comment.