Skip to content

Commit

Permalink
better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Jun 21, 2024
1 parent 5fd20c7 commit 7edbaf6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, test, expect, beforeEach, afterEach } from 'vitest'
import { sum } from './sum'

describe('context', () => {
beforeEach(() => {
throw new Error('failed before each')
})
test('can report failed test', () => {
expect(sum(1, 2)).to.equal(4)
})
test('can report more', () => {
expect(sum(1, 2)).to.equal(3)
})
})

describe('other context', () => {
afterEach(() => {
throw new Error('failed after each')
})
test('can report passed test', () => {
expect(sum(1, 2)).to.equal(3)
})
test('can report more', () => {
expect(sum(1, 2)).to.equal(3)
})
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { describe, test, expect } from 'vitest'
import { describe, test, expect, beforeEach, afterEach } from 'vitest'
import { sum } from './sum'

describe('context', () => {
let preparedValue = 1

describe('test-visibility-failed-suite-first-describe', () => {
beforeEach(() => {
preparedValue = 2
})
test('can report failed test', () => {
expect(sum(1, 2)).to.equal(4)
})
test('can report more', () => {
expect(sum(1, 2)).to.equal(3)
expect(preparedValue).to.equal(2)
})
})

describe('other context', () => {
describe('test-visibility-failed-suite-second-describe', () => {
afterEach(() => {
preparedValue = 1
})
test('can report passed test', () => {
expect(sum(1, 2)).to.equal(3)
})
Expand Down
64 changes: 37 additions & 27 deletions integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,45 @@ versions.forEach((version) => {
)
assert.equal(failedSuite.content.meta[TEST_STATUS], 'fail')

const failedTest = testEvents.find(
({ content: { resource } }) =>
resource === 'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs.can report failed test'
const failedSuiteHooks = testSuiteEvents.find(
suite => suite.content.resource === 'test_suite.ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs'
)
assert.equal(failedSuiteHooks.content.meta[TEST_STATUS], 'fail')

assert.includeMembers(testEvents.map(test => test.content.resource),
[
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs' +
'.test-visibility-failed-suite-first-describe can report failed test ',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs' +
'.test-visibility-failed-suite-first-describe can report more ',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs' +
'.test-visibility-failed-suite-second-describe can report passed test ',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs' +
'.test-visibility-failed-suite-second-describe can report more ',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.context can report passed test ',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.context can report more ',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.other context can report passed test ',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.other context can report more ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.context can report failed test ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.context can report more ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.other context can report passed test ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.other context can report more '
]
)

assert.equal(failedTest.content.meta[TEST_STATUS], 'fail')

const passedTests = testEvents.filter(testEvent => testEvent.content.meta[TEST_STATUS] === 'pass')

assert.includeMembers(passedTests.map(test => test.content.resource), [
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.can report passed test',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.can report more',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.can report passed test',
'ci-visibility/vitest-tests/test-visibility-passed-suite.mjs.can report more',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs.can report more',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs.can report passed test',
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs.can report more'
])

assert.includeMembers(testEvents.map(test => test.content.meta[TEST_STATUS]), [
'pass',
'pass',
'pass',
'pass',
'pass',
'pass',
'pass',
'fail'
])
const failedTests = testEvents.filter(test => test.content.meta[TEST_STATUS] === 'fail')

assert.includeMembers(
failedTests.map(test => test.content.resource),
[
'ci-visibility/vitest-tests/test-visibility-failed-suite.mjs' +
'.test-visibility-failed-suite-first-describe can report failed test ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.context can report failed test ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.context can report more ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.other context can report passed test ',
'ci-visibility/vitest-tests/test-visibility-failed-hooks.mjs.other context can report more '
]
)
// TODO: check error messages
}).then(() => done()).catch(done)

Expand Down

0 comments on commit 7edbaf6

Please sign in to comment.