Skip to content

Commit 5041f4a

Browse files
author
DavertMik
committed
added esm support, fixed tests, added cjs support
1 parent a0a32ee commit 5041f4a

File tree

3 files changed

+467
-47
lines changed

3 files changed

+467
-47
lines changed

index.js

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
const { output } = require('codeceptjs')
1+
import { use, expect, assert } from 'chai';
2+
import chaiExclude from 'chai-exclude';
3+
import { createRequire } from 'module';
24

3-
let chai;
4-
let expect;
5+
const require = createRequire(import.meta.url);
6+
const { output } = require('codeceptjs');
57

6-
import('chai').then((chaiImported) => {
7-
chai = chaiImported;
8-
expect = chai.expect;
9-
// @ts-ignore
10-
chai.use(require('chai-exclude'));
11-
chai.use(require('chai-match-pattern'));
12-
});
8+
use(chaiExclude);
9+
use(require('chai-match-pattern'));
1310

1411
/**
1512
* This helper allows performing assertions based on Chai.
@@ -115,7 +112,7 @@ class ExpectHelper {
115112
expectStartsWith(actualValue, expectedValueToStartWith, customErrorMsg = '') {
116113
// @ts-ignore
117114
output.step(`I expect "${JSON.stringify(actualValue)}" to start with "${JSON.stringify(expectedValueToStartWith)}"`)
118-
return expect(actualValue.startsWith(expectedValueToStartWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to start with "${expectedValueToStartWith}"` : `Expected "${actualValue}" to start with "${expectedValueToStartWith}"`).to.be.true
115+
return assert(actualValue.startsWith(expectedValueToStartWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to start with "${expectedValueToStartWith}"` : `Expected "${actualValue}" to start with "${expectedValueToStartWith}"`)
119116
}
120117

121118
/**
@@ -129,7 +126,7 @@ class ExpectHelper {
129126
output.step(
130127
`I expect "${JSON.stringify(actualValue)}" to not start with "${JSON.stringify(expectedValueToNotStartWith)}"`,
131128
)
132-
return expect(actualValue.startsWith(expectedValueToNotStartWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to start with "${expectedValueToStartWith}"` : `Expected "${actualValue}" not to start with "${expectedValueToNotStartWith}"`).to.be.false
129+
return assert(!actualValue.startsWith(expectedValueToNotStartWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to start with "${expectedValueToNotStartWith}"` : `Expected "${actualValue}" not to start with "${expectedValueToNotStartWith}"`)
133130
}
134131

135132
/**
@@ -140,7 +137,7 @@ class ExpectHelper {
140137
expectEndsWith(actualValue, expectedValueToEndWith, customErrorMsg = '') {
141138
// @ts-ignore
142139
output.step(`I expect "${JSON.stringify(actualValue)}" to end with "${JSON.stringify(expectedValueToEndWith)}"`)
143-
return expect(actualValue.endsWith(expectedValueToEndWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to end with "${expectedValueToEndWith}"` : `Expected "${actualValue}" to end with "${expectedValueToEndWith}"`).to.be.true
140+
return assert(actualValue.endsWith(expectedValueToEndWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" to end with "${expectedValueToEndWith}"` : `Expected "${actualValue}" to end with "${expectedValueToEndWith}"`)
144141
}
145142

146143
/**
@@ -153,7 +150,7 @@ class ExpectHelper {
153150
output.step(
154151
`I expect "${JSON.stringify(actualValue)}" to not end with "${JSON.stringify(expectedValueToNotEndWith)}"`,
155152
)
156-
return expect(actualValue.endsWith(expectedValueToNotEndWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" not to end with "${expectedValueToNotEndWith}"` : `Expected "${actualValue}" not to end with "${expectedValueToNotEndWith}"`).to.be.false
153+
return assert(!actualValue.endsWith(expectedValueToNotEndWith), customErrorMsg ? `${customErrorMsg}: Expected "${actualValue}" not to end with "${expectedValueToNotEndWith}"` : `Expected "${actualValue}" not to end with "${expectedValueToNotEndWith}"`)
157154
}
158155

159156
/**
@@ -176,7 +173,7 @@ class ExpectHelper {
176173
output.step(
177174
`I expect "${JSON.stringify(targetData)}" to match this JSON schema using AJV "${JSON.stringify(jsonSchema)}"`,
178175
)
179-
chai.use(require('chai-json-schema-ajv').create(ajvOptions))
176+
use(require('chai-json-schema-ajv').create(ajvOptions))
180177
return expect(targetData, customErrorMsg).to.be.jsonSchema(jsonSchema)
181178
}
182179

@@ -263,7 +260,7 @@ class ExpectHelper {
263260
expectTrue(targetData, customErrorMsg = '') {
264261
// @ts-ignore
265262
output.step(`I expect "${JSON.stringify(targetData)}" to be true`)
266-
return expect(targetData, customErrorMsg).to.be.true
263+
return expect(targetData, customErrorMsg).to.be.true;
267264
}
268265

269266
/**
@@ -328,7 +325,9 @@ class ExpectHelper {
328325
expectEqualIgnoreCase(actualValue, expectedValue, customErrorMsg = '') {
329326
// @ts-ignore
330327
output.step(`I expect and ingore case "${JSON.stringify(actualValue)}" to equal "${JSON.stringify(expectedValue)}"`)
331-
return expect(actualValue.toLowerCase(), customErrorMsg).to.equal(expectedValue.toLowerCase())
328+
let message = `expected '${actualValue}' to equal '${expectedValue}'`;
329+
if (customErrorMsg) message = `${customErrorMsg}: ${message}`;
330+
return expect(actualValue.toLowerCase(), message).to.equal(expectedValue.toLowerCase())
332331
}
333332

334333
/**
@@ -364,25 +363,29 @@ class ExpectHelper {
364363
* @param {*} fieldsToExclude
365364
* @param {*} [customErrorMsg]
366365
*/
367-
expectDeepEqualExcluding(actualValue, expectedValue, fieldsToExclude, customErrorMsg = '') {
368-
// @ts-ignore
369-
output.step(
370-
`I expect members of "${JSON.stringify(actualValue)}" and "${JSON.stringify(expectedValue)}" JSON objects are deeply equal excluding properties: ${JSON.stringify(fieldsToExclude)}`,
371-
)
372-
373-
const filterFields = (obj, fields) => {
374-
const filteredObj = { ...obj };
375-
fields.forEach(field => {
376-
delete filteredObj[field];
377-
});
378-
return filteredObj;
379-
};
380-
381-
const filteredActualValue = filterFields(actualValue, fieldsToExclude);
382-
const filteredExpectedValue = filterFields(expectedValue, fieldsToExclude);
383-
384-
return expect(filteredActualValue, customErrorMsg).to.deep.equal(filteredExpectedValue);
385-
}
366+
expectDeepEqualExcluding(actualValue, expectedValue, fieldsToExclude, customErrorMsg = '') {
367+
// @ts-ignore
368+
output.step(
369+
`I expect members of "${JSON.stringify(actualValue)}" and "${JSON.stringify(expectedValue)}" JSON objects are deeply equal excluding properties: ${JSON.stringify(fieldsToExclude)}`,
370+
)
371+
372+
const filterFields = (obj, fields = []) => {
373+
const filteredObj = { ...obj };
374+
if (Array.isArray(fields)) {
375+
fields.forEach(field => {
376+
delete filteredObj[field];
377+
});
378+
} else {
379+
delete filteredObj[fields];
380+
}
381+
return filteredObj;
382+
};
383+
384+
const filteredActualValue = filterFields(actualValue, fieldsToExclude);
385+
const filteredExpectedValue = filterFields(expectedValue, fieldsToExclude);
386+
387+
return expect(filteredActualValue, customErrorMsg).to.deep.equal(filteredExpectedValue);
388+
}
386389

387390
/**
388391
* expects a JSON object matches a provided pattern
@@ -405,8 +408,8 @@ class ExpectHelper {
405408
};
406409
const result = matchesPattern(actualValue, expectedPattern);
407410
const errorMessage = customErrorMsg ? `${customErrorMsg}: Expected "${JSON.stringify(actualValue)}" to match the pattern "${JSON.stringify(expectedPattern)}"` : `Expected "${JSON.stringify(actualValue)}" to match the pattern "${JSON.stringify(expectedPattern)}"`;
408-
return expect(result, errorMessage).to.be.true;
411+
return assert(result, errorMessage);
409412
}
410413
}
411414

412-
module.exports = ExpectHelper
415+
export default ExpectHelper

0 commit comments

Comments
 (0)