diff --git a/jest.config.js b/jest.config.js index 4c02a56c2f0a0edcc39ef2ca8a77527d11580e91..63dd1fe331a4ffa8bae597d3fd83e402a965030a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -43,6 +43,7 @@ module.exports = { reporters: [ 'default' ], + setupFiles: ['./jest.global.js'], globals: { describes: parseDescribes() }, @@ -51,4 +52,4 @@ module.exports = { rootDir: __dirname, testMatch: ["/pages/**/*test.[jt]s?(x)"], testPathIgnorePatterns: ['/node_modules/'] -} +} diff --git a/jest.global.js b/jest.global.js new file mode 100644 index 0000000000000000000000000000000000000000..72c2ea90ea6cf64e432a080f8f6ec3882fa5c120 --- /dev/null +++ b/jest.global.js @@ -0,0 +1,44 @@ +const { + readFileSync, + readdirSync +} = require('fs') +const { + extname, + resolve +} = require('path') + +module.exports = async function (globalConfig, projectConfig) { + + const describeRE = /describe\(["|'](.*)["|']/ + const testsRE = /test\(["|'](.*)["|']/g + + function parse(content) { + const describes = content.match(describeRE) + if (!describes) { + return + } + const describe = describes[1] + const tests = [] + let test + while (test = testsRE.exec(content)) { + tests.push(test[1]) + } + return { + describe, + tests + } + } + + function parseDescribes() { + const dir = resolve(__dirname, 'uni_modules/uts-tests/utssdk') + const describes = [] + readdirSync(dir).forEach(file => { + if (extname(file) === '.uts') { + describes.push(parse(readFileSync(resolve(dir, file), 'utf8'))) + } + }) + return describes + }; + + global.describes = parseDescribes(); +}; \ No newline at end of file