codeceptjs 4.0.0-rc.21 → 4.0.0-rc.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,303 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import os from 'os'
4
+ import { mkdirp } from 'mkdirp'
5
+ import { DOMImplementation, XMLSerializer } from '@xmldom/xmldom'
6
+
7
+ import event from '../event.js'
8
+ import store from '../store.js'
9
+ import output from '../output.js'
10
+
11
+ const defaultConfig = {
12
+ outputName: 'report.xml',
13
+ output: null,
14
+ testGroupName: 'CodeceptJS',
15
+ attachSteps: true,
16
+ attachMeta: true,
17
+ stepsInFailure: true,
18
+ }
19
+
20
+ const INVALID_XML_CHARS = new RegExp('[\\u0000-\\u0008\\u000B\\u000C\\u000E-\\u001F\\uFFFE\\uFFFF]', 'g')
21
+
22
+ /**
23
+ *
24
+ * Generates a JUnit-compatible XML report after a test run.
25
+ *
26
+ * Unlike Mocha's `mocha-junit-reporter`, this plugin understands CodeceptJS steps and substeps.
27
+ * For every `<testcase>` it includes:
28
+ *
29
+ * * `<properties>` — the test's meta information: every `meta` key from `Scenario('...', { meta })`, plus its `tags` and `retries`
30
+ * * `<system-out>` — an indented step/substep log (substeps are nested under their meta step); only failed steps are marked
31
+ * * `<failure>` — for failed tests: the error message, type, stack trace and (optionally) the step trace
32
+ *
33
+ * The produced file is consumable by Jenkins, GitLab CI, CircleCI, GitHub Actions test reporters, etc.
34
+ *
35
+ * #### Configuration
36
+ *
37
+ * ```js
38
+ * "plugins": {
39
+ * "junitReporter": {
40
+ * "enabled": true
41
+ * }
42
+ * }
43
+ * ```
44
+ *
45
+ * Possible config options:
46
+ *
47
+ * * `outputName`: file name for the report. Default: `report.xml`.
48
+ * * `output`: directory where the report is stored, relative to the project root. Default: the `output` directory.
49
+ * * `testGroupName`: value of the `name` attribute on the root `<testsuites>` element. Default: `CodeceptJS`.
50
+ * * `attachMeta`: add the test's meta information (`meta` keys, `tags`, `retries`) as `<properties>`. Default: true.
51
+ * * `attachSteps`: add the step/substep log as `<system-out>`. Default: true.
52
+ * * `stepsInFailure`: append the step trace to the `<failure>` body. Default: true.
53
+ *
54
+ * CLI examples:
55
+ *
56
+ * ```
57
+ * npx codeceptjs run -p junitReporter
58
+ * npx codeceptjs run -p junitReporter:outputName=junit.xml
59
+ * ```
60
+ *
61
+ * > ℹ When running with `run-workers`, steps are serialized between processes and substep nesting is flattened.
62
+ *
63
+ * @param {*} config
64
+ */
65
+ export default function (config = {}) {
66
+ config = Object.assign({}, defaultConfig, config)
67
+
68
+ let written = false
69
+
70
+ const writeReport = result => {
71
+ if (written) return
72
+ if (!result || !Array.isArray(result.tests)) return
73
+ written = true
74
+
75
+ const dir = config.output ? path.resolve(store.codeceptDir || process.cwd(), config.output) : store.outputDir || process.cwd()
76
+ mkdirp.sync(dir)
77
+ const file = path.join(dir, config.outputName)
78
+
79
+ fs.writeFileSync(file, buildXml(result, config))
80
+ output.plugin('junitReporter', `JUnit report saved to ${file}`)
81
+ }
82
+
83
+ event.dispatcher.on(event.all.result, writeReport)
84
+ event.dispatcher.on(event.workers.result, writeReport)
85
+ }
86
+
87
+ function buildXml(result, config) {
88
+ const doc = new DOMImplementation().createDocument(null, null, null)
89
+ const suites = groupBySuite(result.tests)
90
+
91
+ const root = doc.createElement('testsuites')
92
+ setAttr(root, 'name', config.testGroupName)
93
+ setAttr(root, 'tests', result.tests.length)
94
+ setAttr(root, 'failures', countState(result.tests, 'failed'))
95
+ setAttr(root, 'skipped', countSkipped(result.tests))
96
+ setAttr(root, 'errors', 0)
97
+ setAttr(root, 'time', toSeconds(sumDuration(result.tests)))
98
+ setAttr(root, 'timestamp', toIso(result.stats && result.stats.start))
99
+ doc.appendChild(root)
100
+
101
+ suites.forEach((tests, index) => {
102
+ const suite = tests[0] && tests[0].parent
103
+ const suiteName = (suite && suite.title) || 'Tests'
104
+ const suiteFile = (suite && suite.file) || (tests[0] && tests[0].file) || ''
105
+
106
+ const suiteEl = doc.createElement('testsuite')
107
+ setAttr(suiteEl, 'name', suiteName)
108
+ setAttr(suiteEl, 'id', index)
109
+ setAttr(suiteEl, 'tests', tests.length)
110
+ setAttr(suiteEl, 'failures', countState(tests, 'failed'))
111
+ setAttr(suiteEl, 'skipped', countSkipped(tests))
112
+ setAttr(suiteEl, 'errors', 0)
113
+ setAttr(suiteEl, 'time', toSeconds(sumDuration(tests)))
114
+ setAttr(suiteEl, 'timestamp', toIso(suite && suite.startedAt))
115
+ setAttr(suiteEl, 'hostname', os.hostname())
116
+ if (suiteFile) setAttr(suiteEl, 'file', suiteFile)
117
+ root.appendChild(suiteEl)
118
+
119
+ for (const test of tests) {
120
+ suiteEl.appendChild(buildTestCase(doc, test, suiteName, config))
121
+ }
122
+ })
123
+
124
+ return '<?xml version="1.0" encoding="UTF-8"?>\n' + new XMLSerializer().serializeToString(doc) + '\n'
125
+ }
126
+
127
+ function buildTestCase(doc, test, suiteName, config) {
128
+ const testEl = doc.createElement('testcase')
129
+ setAttr(testEl, 'name', test.title || '(no title)')
130
+ setAttr(testEl, 'classname', suiteName)
131
+ setAttr(testEl, 'time', toSeconds(test.duration || 0))
132
+ const file = test.file || (test.parent && test.parent.file)
133
+ if (file) setAttr(testEl, 'file', file)
134
+
135
+ if (config.attachMeta) {
136
+ const properties = metaProperties(test)
137
+ if (properties.length) {
138
+ const propertiesEl = doc.createElement('properties')
139
+ for (const [name, value] of properties) {
140
+ const prop = doc.createElement('property')
141
+ setAttr(prop, 'name', name)
142
+ setAttr(prop, 'value', value)
143
+ propertiesEl.appendChild(prop)
144
+ }
145
+ testEl.appendChild(propertiesEl)
146
+ }
147
+ }
148
+
149
+ const flat = flattenSteps(Array.isArray(test.steps) ? test.steps : [])
150
+
151
+ if (test.state === 'skipped' || test.state === 'pending') {
152
+ const skipped = doc.createElement('skipped')
153
+ const reason = skipReason(test)
154
+ if (reason) setAttr(skipped, 'message', reason)
155
+ testEl.appendChild(skipped)
156
+ } else if (test.state === 'failed') {
157
+ const err = test.err || {}
158
+ const failure = doc.createElement('failure')
159
+ setAttr(failure, 'message', err.message || 'Test failed')
160
+ setAttr(failure, 'type', err.name || 'Error')
161
+ let body = err.stack || err.message || 'Test failed'
162
+ if (config.stepsInFailure && flat.length) {
163
+ body += '\n\nSteps:\n' + flat.map(stepLogLine).join('\n')
164
+ }
165
+ failure.appendChild(doc.createTextNode(cleanText(body)))
166
+ testEl.appendChild(failure)
167
+ }
168
+
169
+ if (config.attachSteps && flat.length) {
170
+ const out = doc.createElement('system-out')
171
+ out.appendChild(doc.createTextNode(cleanText(flat.map(stepLogLine).join('\n'))))
172
+ testEl.appendChild(out)
173
+ }
174
+
175
+ return testEl
176
+ }
177
+
178
+ function metaProperties(test) {
179
+ const props = []
180
+ const meta = test.meta || {}
181
+ for (const key of Object.keys(meta)) {
182
+ if (meta[key] === undefined || meta[key] === null) continue
183
+ props.push([key, stringifyMeta(meta[key])])
184
+ }
185
+ if (Array.isArray(test.tags) && test.tags.length) {
186
+ props.push(['tags', test.tags.join(' ')])
187
+ }
188
+ if (test.retries > 0 || test.retryNum > 0) {
189
+ props.push(['retries', String(test.retryNum || test.retries)])
190
+ }
191
+ return props
192
+ }
193
+
194
+ function stringifyMeta(value) {
195
+ if (typeof value === 'string') return value
196
+ if (typeof value === 'number' || typeof value === 'boolean') return String(value)
197
+ try {
198
+ return JSON.stringify(value)
199
+ } catch (err) {
200
+ return String(value)
201
+ }
202
+ }
203
+
204
+ function flattenSteps(steps) {
205
+ const out = []
206
+ let prevChain = []
207
+
208
+ for (const step of steps) {
209
+ const chain = metaChain(step)
210
+
211
+ let common = 0
212
+ while (common < chain.length && common < prevChain.length && chain[common].key === prevChain[common].key) common++
213
+
214
+ for (let d = common; d < chain.length; d++) {
215
+ out.push({ depth: d, step: chain[d].step })
216
+ }
217
+ out.push({ depth: chain.length, step })
218
+ prevChain = chain
219
+ }
220
+
221
+ return out
222
+ }
223
+
224
+ function metaChain(step) {
225
+ const chain = []
226
+ let meta = step && step.metaStep
227
+ while (meta) {
228
+ chain.unshift({ step: meta, key: meta })
229
+ meta = meta.metaStep
230
+ }
231
+ if (!chain.length && step && step.parent && step.parent.title) {
232
+ chain.push({ step: { title: step.parent.title, status: step.status }, key: `meta:${step.parent.title}` })
233
+ }
234
+ return chain
235
+ }
236
+
237
+ function stepLogLine(entry) {
238
+ const indent = ' '.repeat(entry.depth)
239
+ const mark = entry.step && entry.step.status === 'failed' ? '[FAILED] ' : ''
240
+ return `${indent}${mark}${stepText(entry.step)} (${stepDuration(entry.step)}ms)`
241
+ }
242
+
243
+ function stepText(step) {
244
+ if (step && typeof step.toString === 'function' && step.toString !== Object.prototype.toString) return step.toString()
245
+ return (step && (step.title || step.name)) || 'step'
246
+ }
247
+
248
+ function stepDuration(step) {
249
+ if (!step) return 0
250
+ if (typeof step.duration === 'number' && step.duration >= 0) return step.duration
251
+ if (step.startTime && step.endTime) return Math.max(0, step.endTime - step.startTime)
252
+ return 0
253
+ }
254
+
255
+ function groupBySuite(tests) {
256
+ const groups = []
257
+ const byKey = new Map()
258
+ for (const test of tests) {
259
+ const key = test.parent || test
260
+ if (!byKey.has(key)) {
261
+ const list = []
262
+ byKey.set(key, list)
263
+ groups.push(list)
264
+ }
265
+ byKey.get(key).push(test)
266
+ }
267
+ return groups
268
+ }
269
+
270
+ function skipReason(test) {
271
+ if (test.opts && test.opts.skipInfo && test.opts.skipInfo.message) return test.opts.skipInfo.message
272
+ if (test.meta && test.meta.skipReason) return test.meta.skipReason
273
+ return ''
274
+ }
275
+
276
+ function countState(tests, state) {
277
+ return tests.filter(t => t.state === state).length
278
+ }
279
+
280
+ function countSkipped(tests) {
281
+ return tests.filter(t => t.state === 'skipped' || t.state === 'pending').length
282
+ }
283
+
284
+ function sumDuration(tests) {
285
+ return tests.reduce((sum, t) => sum + (t.duration || 0), 0)
286
+ }
287
+
288
+ function toSeconds(ms) {
289
+ return (Math.max(0, ms) / 1000).toFixed(3)
290
+ }
291
+
292
+ function toIso(value) {
293
+ const date = value ? new Date(value) : new Date()
294
+ return Number.isNaN(date.getTime()) ? new Date().toISOString() : date.toISOString()
295
+ }
296
+
297
+ function cleanText(text) {
298
+ return String(text == null ? '' : text).replace(INVALID_XML_CHARS, '')
299
+ }
300
+
301
+ function setAttr(el, name, value) {
302
+ el.setAttribute(name, cleanText(value))
303
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codeceptjs",
3
- "version": "4.0.0-rc.21",
3
+ "version": "4.0.0-rc.22",
4
4
  "type": "module",
5
5
  "description": "Supercharged End 2 End Testing Framework for NodeJS",
6
6
  "keywords": [
@@ -1,265 +0,0 @@
1
- ---
2
- permalink: /internal-api
3
- title: Internal API
4
- ---
5
-
6
- ## Concepts
7
-
8
- In this guide we will overview the internal API of CodeceptJS.
9
- This knowledge is required for customization, writing plugins, etc.
10
-
11
- CodeceptJS exposes its internal API as named exports of the `codeceptjs` package. Import only what you need:
12
-
13
- ```js
14
- import { recorder, event, output, container, config } from 'codeceptjs'
15
- ```
16
-
17
- > Older code may have relied on a global `codeceptjs` object (`const { recorder } = codeceptjs`). That global only exists under `noGlobals: false` (the deprecated 3.x default) — prefer named imports.
18
-
19
- These internal objects are available:
20
-
21
- * [`codecept`](https://github.com/Codeception/CodeceptJS/blob/master/lib/codecept.js): test runner class
22
- * [`config`](https://github.com/Codeception/CodeceptJS/blob/master/lib/config.js): current codecept config
23
- * [`event`](https://github.com/Codeception/CodeceptJS/blob/master/lib/event.js): event listener
24
- * [`recorder`](https://github.com/Codeception/CodeceptJS/blob/master/lib/recorder.js): global promise chain
25
- * [`output`](https://github.com/Codeception/CodeceptJS/blob/master/lib/output.js): internal printer
26
- * [`container`](https://github.com/Codeception/CodeceptJS/blob/master/lib/container.js): dependency injection container for tests, includes current helpers and support objects
27
- * [`helper`](https://github.com/Codeception/CodeceptJS/blob/master/lib/helper.js): basic helper class
28
- * [`actor`](https://github.com/Codeception/CodeceptJS/blob/master/lib/actor.js): basic actor (I) class
29
-
30
- [API reference](https://github.com/Codeception/CodeceptJS/tree/master/docs/api) is available on GitHub.
31
- Also please check the source code of corresponding modules.
32
-
33
- ### Container
34
-
35
- CodeceptJS has a dependency injection container with helpers and support objects.
36
- They can be retrieved from the container:
37
-
38
- ```js
39
- import { container } from 'codeceptjs';
40
-
41
- // get object with all helpers
42
- const helpers = container.helpers();
43
-
44
- // get helper by name
45
- const { WebDriver } = container.helpers();
46
-
47
- // get support objects
48
- const supportObjects = container.support();
49
-
50
- // get support object by name
51
- const { UserPage } = container.support();
52
-
53
- // get all registered plugins
54
- const plugins = container.plugins();
55
- ```
56
-
57
- New objects can also be added to container in runtime:
58
-
59
- ```js
60
- import { container } from 'codeceptjs';
61
- import UserPage from './pages/user.js';
62
-
63
- container.append({
64
- helpers: { // add helper
65
- MyHelper: new MyHelper({ config1: 'val1' });
66
- },
67
- support: { // add page object
68
- UserPage,
69
- }
70
- })
71
- ```
72
-
73
- > Use this trick to define custom objects inside `boostrap` script
74
-
75
- The container also contains the current Mocha instance:
76
-
77
- ```js
78
- const mocha = container.mocha();
79
- ```
80
-
81
- ### Event Listeners
82
-
83
- CodeceptJS provides a module with an [event dispatcher and set of predefined events](https://github.com/Codeception/CodeceptJS/blob/master/lib/event.js).
84
-
85
- It can be required from codeceptjs package if it is installed locally.
86
-
87
- ```js
88
- import { event } from 'codeceptjs';
89
-
90
- export default function() {
91
-
92
- event.dispatcher.on(event.test.before, function (test) {
93
-
94
- console.log('--- I am before test --');
95
-
96
- });
97
- }
98
- ```
99
-
100
- Available events:
101
-
102
- * `event.test.before(test)` - *async* when `Before` hooks from helpers and from test is executed
103
- * `event.test.after(test)` - *async* after each test
104
- * `event.test.started(test)` - *sync* at the very beginning of a test.
105
- * `event.test.passed(test)` - *sync* when test passed
106
- * `event.test.failed(test, error)` - *sync* when test failed
107
- * `event.test.finished(test)` - *sync* when test finished
108
- * `event.suite.before(suite)` - *async* before a suite
109
- * `event.suite.after(suite)` - *async* after a suite
110
- * `event.step.before(step)` - *async* when the step is scheduled for execution
111
- * `event.step.after(step)`- *async* after a step
112
- * `event.step.started(step)` - *sync* when step starts.
113
- * `event.step.passed(step)` - *sync* when step passed.
114
- * `event.step.failed(step, err)` - *sync* when step failed.
115
- * `event.step.finished(step)` - *sync* when step finishes.
116
- * `event.step.comment(step)` - *sync* fired for comments like `I.say`.
117
- * `event.all.before` - before running tests
118
- * `event.all.after` - after running tests
119
- * `event.all.result` - when results are printed
120
- * `event.workers.before` - before spawning workers in parallel run
121
- * `event.workers.after` - after workers finished in parallel run
122
- * `event.workers.result` - test results after workers finished in parallel run
123
-
124
-
125
- > *sync* - means that event is fired in the moment of the action happening.
126
- *async* - means that event is fired when an action is scheduled. Use `recorder` to schedule your actions.
127
-
128
- For further reference look for [currently available listeners](https://github.com/Codeception/CodeceptJS/tree/master/lib/listener) using the event system.
129
-
130
-
131
- ### Recorder
132
-
133
- To inject asynchronous functions in a test or before/after a test you can subscribe to corresponding event and register a function inside a recorder object. [Recorder](https://github.com/Codeception/CodeceptJS/blob/master/lib/recorder.js) represents a global promises chain.
134
-
135
- Provide a function in the first parameter, a function must be async or must return a promise:
136
-
137
- ```js
138
- import { event, recorder } from 'codeceptjs';
139
- import request from 'request';
140
-
141
- export default function() {
142
-
143
- event.dispatcher.on(event.test.before, function (test) {
144
-
145
- recorder.add('create fixture data via API', function() {
146
- return new Promise((doneFn, errFn) => {
147
- request({
148
- baseUrl: 'http://api.site.com/',
149
- method: 'POST',
150
- url: '/users',
151
- json: { name: 'john', email: 'john@john.com' }
152
- }), (err, httpResponse, body) => {
153
- if (err) return errFn(err);
154
- doneFn();
155
- }
156
- });
157
- }
158
- });
159
- }
160
- ```
161
-
162
- ### Config
163
-
164
- CodeceptJS config can be accessed from `import { config } from 'codeceptjs'` then `config.get()`:
165
-
166
- ```js
167
- import { config } from 'codeceptjs';
168
-
169
- // config object has access to all values of the current config file
170
-
171
- if (config.get().myKey == 'value') {
172
- // run something
173
- }
174
- ```
175
-
176
-
177
- ### Output
178
-
179
- Output module provides four verbosity levels. Depending on the mode you can have different information printed using corresponding functions.
180
-
181
- * `default`: prints basic information using `output.print`
182
- * `steps`: toggled by `--steps` option, prints step execution
183
- * `debug`: toggled by `--debug` option, prints steps, and debug information with `output.debug`
184
- * `verbose`: toggled by `--verbose` prints debug information and internal logs with `output.log`
185
-
186
- It is recommended to avoid `console.log` and use output.* methods for printing.
187
-
188
- ```js
189
- import { output } from 'codeceptjs';
190
-
191
- output.print('This is basic information');
192
- output.debug('This is debug information');
193
- output.log('This is verbose logging information');
194
- ```
195
-
196
- #### Test Object
197
-
198
- The test events are providing a test object with following properties:
199
-
200
- * `title` title of the test
201
- * `body` test function as a string
202
- * `opts` additional test options like retries, and others
203
- * `pending` true if test is scheduled for execution and false if a test has finished
204
- * `tags` array of tags for this test
205
- * `artifacts` list of files attached to this test. Screenshots, videos and other files can be saved here and shared accross different reporters
206
- * `file` path to a file with a test
207
- * `steps` array of executed steps (available only in `test.passed`, `test.failed`, `test.finished` event)
208
- * `skipInfo` additional test options when test skipped
209
- * * `message` string with reason for skip
210
- * * `description` string with test body
211
- and others
212
-
213
- #### Step Object
214
-
215
- Step events provide step objects with following fields:
216
-
217
- * `name` name of a step, like 'see', 'click', and others
218
- * `actor` current actor, in most cases it is `I`
219
- * `helper` current helper instance used to execute this step
220
- * `helperMethod` corresponding helper method, in most cases is the same as `name`
221
- * `status` status of a step (passed or failed)
222
- * `prefix` if a step is executed inside `within` block contain within text, like: 'Within .js-signup-form'.
223
- * `args` passed arguments
224
-
225
- Whenever you execute tests with `--verbose` option you will see registered events and promises executed by a recorder.
226
-
227
- ## Custom Runner
228
-
229
- You can run CodeceptJS tests from your script.
230
-
231
- ```js
232
- import { codecept as Codecept } from 'codeceptjs';
233
-
234
- // define main config
235
- const config = {
236
- helpers: {
237
- WebDriver: {
238
- browser: 'chrome',
239
- url: 'http://localhost'
240
- }
241
- }
242
- };
243
-
244
- const opts = { steps: true };
245
-
246
- // run CodeceptJS inside async function
247
- (async () => {
248
- const codecept = new Codecept(config, options);
249
- codecept.init(__dirname);
250
-
251
- try {
252
- await codecept.bootstrap();
253
- codecept.loadTests('**_test.js');
254
- // run tests
255
- await codecept.run(test);
256
- } catch (err) {
257
- printError(err);
258
- process.exitCode = 1;
259
- } finally {
260
- await codecept.teardown();
261
- }
262
- })();
263
- ```
264
-
265
- > Also, you can run tests inside workers in a custom scripts. Please refer to the [parallel execution](/parallel) guide for more details.