Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test-visibility] Add support for vitest #4415

Merged
merged 31 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0268820
add vitest support wip
juan-fernandez Jun 18, 2024
4b03ee1
better vitest support
juan-fernandez Jun 18, 2024
689d9aa
wip
juan-fernandez Jun 18, 2024
6512002
clean up
juan-fernandez Jun 19, 2024
d953763
clean up
juan-fernandez Jun 19, 2024
0d4f4ac
add e2e tests
juan-fernandez Jun 19, 2024
17de859
support 1.6.0 too
juan-fernandez Jun 19, 2024
b1ce709
clean up and add error handling
juan-fernandez Jun 20, 2024
81d8a1c
add session error message
juan-fernandez Jun 20, 2024
8c49dbd
add ci job
juan-fernandez Jun 20, 2024
b2a163e
remove volta
juan-fernandez Jun 20, 2024
6213755
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 20, 2024
420e0dd
support for before after all hooks
juan-fernandez Jun 21, 2024
e5ae80f
support beforeeach and aftereach
juan-fernandez Jun 21, 2024
5fd20c7
add test source file
juan-fernandez Jun 21, 2024
7edbaf6
better tests
juan-fernandez Jun 21, 2024
482db61
ts definitions
juan-fernandez Jun 24, 2024
cb580df
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 24, 2024
ba5088c
add support for skipped tests
juan-fernandez Jun 24, 2024
5c570a9
update comment
juan-fernandez Jun 24, 2024
1ddeee8
do not use iitm version and add wildcard logic
juan-fernandez Jun 25, 2024
95c5286
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 25, 2024
aa20aae
static list of esm plugins
juan-fernandez Jun 25, 2024
5551e63
add safeguard hook
juan-fernandez Jun 25, 2024
c15e63c
better regex logic
juan-fernandez Jun 25, 2024
ee2b032
remove unused var
juan-fernandez Jun 25, 2024
0d60e30
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 27, 2024
37f33e7
move esm first logic to hooks
juan-fernandez Jun 28, 2024
b29cfdb
remove dependency on getport
juan-fernandez Jun 28, 2024
6bee656
adding checks for hasSubscriber and asyncresource
juan-fernandez Jun 28, 2024
44ed749
Merge branch 'master' into juan-fernandez/add-vitest-support
juan-fernandez Jun 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ jobs:
CYPRESS_VERSION: ${{ matrix.cypress-version }}
NODE_OPTIONS: '-r ./ci/init'

integration-vitest:
runs-on: ubuntu-latest
env:
DD_SERVICE: dd-trace-js-integration-tests
DD_CIVISIBILITY_AGENTLESS_ENABLED: 1
DD_API_KEY: ${{ secrets.DD_API_KEY_CI_APP }}
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/node/setup
- run: yarn install
- uses: actions/setup-node@v3
with:
node-version: 20
- run: yarn test:integration:vitest
env:
NODE_OPTIONS: '-r ./ci/init'

lint:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion ext/exporters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare const exporters: {
DATADOG: 'datadog',
AGENT_PROXY: 'agent_proxy',
JEST_WORKER: 'jest_worker',
CUCUMBER_WORKER: 'cucumber_worker'
CUCUMBER_WORKER: 'cucumber_worker',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated fix

MOCHA_WORKER: 'mocha_worker'
}

Expand Down
3 changes: 3 additions & 0 deletions integration-tests/ci-visibility/vitest-tests/sum.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sum (a, b) {
return a + b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, test, expect } from 'vitest'
import { sum } from './sum'

describe('context', () => {
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', () => {
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
@@ -0,0 +1,20 @@
import { describe, test, expect } from 'vitest'
import { sum } from './sum'

describe('context', () => {
test('can report passed test', () => {
expect(sum(1, 2)).to.equal(3)
})
test('can report more', () => {
expect(sum(1, 2)).to.equal(3)
})
})

describe('other context', () => {
test('can report passed test', () => {
expect(sum(1, 2)).to.equal(3)
})
test('can report more', () => {
expect(sum(1, 2)).to.equal(3)
})
})
9 changes: 9 additions & 0 deletions integration-tests/vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'vite'

export default defineConfig({
test: {
include: [
'ci-visibility/vitest-tests/test-visibility*'
]
}
})
116 changes: 116 additions & 0 deletions integration-tests/vitest/vitest.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict'

const { exec } = require('child_process')

const getPort = require('get-port')
const { assert } = require('chai')

const {
createSandbox,
getCiVisAgentlessConfig
} = require('../helpers')
const { FakeCiVisIntake } = require('../ci-visibility-intake')
const {
TEST_STATUS,
TEST_TYPE
} = require('../../packages/dd-trace/src/plugins/util/test')

// tested with 1.6.0
const versions = ['latest']

versions.forEach((version) => {
describe(`vitest@${version}`, () => {
let sandbox, cwd, receiver, childProcess

before(async function () {
sandbox = await createSandbox([`vitest@${version}`], true)
cwd = sandbox.folder
})

after(async () => {
await sandbox.remove()
})

beforeEach(async function () {
const port = await getPort()
simon-id marked this conversation as resolved.
Show resolved Hide resolved
receiver = await new FakeCiVisIntake(port).start()
})

afterEach(async () => {
childProcess.kill()
await receiver.stop()
})

it('can run and report tests', (done) => {
receiver.gatherPayloadsMaxTimeout(({ url }) => url === '/api/v2/citestcycle', payloads => {
const events = payloads.flatMap(({ payload }) => payload.events)

const testSessionEvent = events.find(event => event.type === 'test_session_end')
const testModuleEvent = events.find(event => event.type === 'test_module_end')
const testSuiteEvents = events.filter(event => event.type === 'test_suite_end')
const testEvents = events.filter(event => event.type === 'test')

assert.include(testSessionEvent.content.resource, 'test_session.vitest run')
assert.equal(testSessionEvent.content.meta[TEST_STATUS], 'fail')
assert.include(testModuleEvent.content.resource, 'test_module.vitest run')
assert.equal(testModuleEvent.content.meta[TEST_STATUS], 'fail')
assert.equal(testSessionEvent.content.meta[TEST_TYPE], 'test')
assert.equal(testModuleEvent.content.meta[TEST_TYPE], 'test')

const passedSuite = testSuiteEvents.find(
suite => suite.content.resource === 'test_suite.ci-visibility/vitest-tests/test-visibility-passed-suite.mjs'
)
assert.equal(passedSuite.content.meta[TEST_STATUS], 'pass')

const failedSuite = testSuiteEvents.find(
suite => suite.content.resource === 'test_suite.ci-visibility/vitest-tests/test-visibility-failed-suite.mjs'
)
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'
)

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'
])
// TODO: check error messages
}).then(() => done()).catch(done)

childProcess = exec(
'./node_modules/.bin/vitest run',
{
cwd,
env: {
...getCiVisAgentlessConfig(receiver.port),
// maybe only in node@20
NODE_OPTIONS: '--import dd-trace/register.js -r dd-trace/ci/init' // ESM requires more stuff
},
stdio: 'pipe'
}
)
})
})
})
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"test:integration:selenium": "mocha --colors --timeout 30000 -r \"packages/dd-trace/test/setup/core.js\" \"integration-tests/selenium/*.spec.js\"",
"test:integration:profiler": "mocha --colors --timeout 90000 -r \"packages/dd-trace/test/setup/core.js\" \"integration-tests/profiler/*.spec.js\"",
"test:integration:serverless": "mocha --colors --timeout 30000 -r \"packages/dd-trace/test/setup/core.js\" \"integration-tests/serverless/*.spec.js\"",
"test:integration:vitest": "mocha --colors --timeout 30000 -r \"packages/dd-trace/test/setup/core.js\" \"integration-tests/vitest/*.spec.js\"",
"test:integration:plugins": "mocha --colors --exit -r \"packages/dd-trace/test/setup/mocha.js\" \"packages/datadog-plugin-@($(echo $PLUGINS))/test/integration-test/**/*.spec.js\"",
"test:unit:plugins": "mocha --colors --exit -r \"packages/dd-trace/test/setup/mocha.js\" \"packages/datadog-instrumentations/test/@($(echo $PLUGINS)).spec.js\" \"packages/datadog-plugin-@($(echo $PLUGINS))/test/**/*.spec.js\" --exclude \"packages/datadog-plugin-@($(echo $PLUGINS))/test/integration-test/**/*.spec.js\"",
"test:shimmer": "mocha --colors 'packages/datadog-shimmer/test/**/*.spec.js'",
Expand Down Expand Up @@ -81,7 +82,7 @@
"crypto-randomuuid": "^1.0.0",
"dc-polyfill": "^0.1.4",
"ignore": "^5.2.4",
"import-in-the-middle": "^1.7.4",
"import-in-the-middle": "https://github.com/DataDog/import-in-the-middle#dbaca128f6eb517d71bcf7e6b6b45f21a07128a4",
juan-fernandez marked this conversation as resolved.
Show resolved Hide resolved
"int64-buffer": "^0.1.9",
"istanbul-lib-coverage": "3.2.0",
"jest-docblock": "^29.7.0",
Expand Down Expand Up @@ -137,5 +138,8 @@
"sinon-chai": "^3.7.0",
"tap": "^16.3.7",
"tape": "^5.6.5"
},
"volta": {
"node": "20.14.0"
}
}
3 changes: 2 additions & 1 deletion packages/datadog-instrumentations/src/helpers/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ function Hook (modules, onrequire) {
const parts = [moduleBaseDir, moduleName].filter(v => v)
const filename = path.join(...parts)

if (this._patched[filename]) return moduleExports
// this does not seem to work with IITM: the same file seems to import different stuff
// if (this._patched[filename]) return moduleExports
juan-fernandez marked this conversation as resolved.
Show resolved Hide resolved

this._patched[filename] = true

Expand Down
2 changes: 2 additions & 0 deletions packages/datadog-instrumentations/src/helpers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
'@opentelemetry/sdk-trace-node': () => require('../otel-sdk-trace'),
'@redis/client': () => require('../redis'),
'@smithy/smithy-client': () => require('../aws-sdk'),
'@vitest/runner': () => require('../vitest'),
aerospike: () => require('../aerospike'),
amqp10: () => require('../amqp10'),
amqplib: () => require('../amqplib'),
Expand Down Expand Up @@ -110,6 +111,7 @@ module.exports = {
sharedb: () => require('../sharedb'),
tedious: () => require('../tedious'),
undici: () => require('../undici'),
vitest: () => require('../vitest'),
when: () => require('../when'),
winston: () => require('../winston')
}
Loading