Skip to content

Commit 63f4eb2

Browse files
author
DavertMik
committed
fixed REST tests
1 parent aa022c6 commit 63f4eb2

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

lib/helper/ApiDataFactory.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ class ApiDataFactory extends Helper {
265265
* @param {*} [options] options for programmatically generate the attributes
266266
* @returns {Promise<*>}
267267
*/
268-
have(factory, params, options) {
269-
const item = this._createItem(factory, params, options)
268+
async have(factory, params, options) {
269+
const item = await this._createItem(factory, params, options)
270270
this.debug(`Creating ${factory} ${JSON.stringify(item)}`)
271271
return this._requestCreate(factory, item)
272272
}
@@ -298,19 +298,22 @@ class ApiDataFactory extends Helper {
298298
return Promise.all(promises)
299299
}
300300

301-
_createItem(model, data, options) {
301+
async _createItem(model, data, options) {
302302
if (!this.factories[model]) {
303303
throw new Error(`Factory ${model} is not defined in config`)
304304
}
305305
let modulePath = this.factories[model].factory
306306
try {
307307
try {
308-
require.resolve(modulePath)
308+
// Try to resolve the path as-is first
309+
await import.meta.resolve(modulePath)
309310
} catch (e) {
311+
// If not found, try relative to codecept_dir
310312
modulePath = path.join(global.codecept_dir, modulePath)
311313
}
312314
// check if the new syntax `export default new Factory()` is used and loads the builder, otherwise loads the module that used old syntax `module.exports = new Factory()`.
313-
const builder = require(modulePath).default || require(modulePath)
315+
const module = await import(modulePath)
316+
const builder = module.default || module
314317
return builder.build(data, options)
315318
} catch (err) {
316319
throw new Error(`Couldn't load factory file from ${modulePath}, check that

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"repository": "Codeception/codeceptjs",
4545
"scripts": {
46-
"json-server": "json-server test/data/rest/db.json --host 0.0.0.0 -p 8010 --watch -m test/data/rest/headers.js",
46+
"json-server": "json-server test/data/rest/db.json --host 0.0.0.0 -p 8010 --watch -m test/data/rest/headers.cjs",
4747
"json-server:graphql": "node test/data/graphql/index.js",
4848
"lint": "eslint bin/ examples/ lib/ test/ translations/ runok.cjs",
4949
"lint-fix": "eslint bin/ examples/ lib/ test/ translations/ runok.cjs --fix",

test/data/rest/db.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
{
2+
"comments": [],
23
"posts": [
34
{
45
"id": 1,
56
"title": "json-server",
67
"author": "davert"
78
}
8-
],
9-
"user": {
10-
"name": "john",
11-
"password": "123456"
12-
}
13-
}
9+
]
10+
}

test/data/rest/headers.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = (req, res, next) => {
2+
if (req.path !== '/headers') return next()
3+
res.json(req.headers)
4+
}

test/data/rest/headers.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/rest/ApiDataFactory_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import path from 'path'
22
import fs from 'fs'
3+
import { fileURLToPath } from 'url'
4+
5+
const __filename = fileURLToPath(import.meta.url)
6+
const __dirname = path.dirname(__filename)
37

48
import '../support/setup.js'
59
import TestHelper from '../support/TestHelper.js'

test/support/setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import chai from 'chai'
2+
chai.should()

0 commit comments

Comments
 (0)