From 8e254a8cc78d2866a209b8c8f87df1b7fa2652f2 Mon Sep 17 00:00:00 2001 From: wangjinxin613 <1749574663@qq.com> Date: Mon, 4 Mar 2024 14:23:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20jest=20=E9=AB=98?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=85=A8=E5=B1=80=E5=8F=98=E9=87=8F=E6=89=BE?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jest.config.js | 3 ++- jest.global.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 jest.global.js diff --git a/jest.config.js b/jest.config.js index 4c02a56..63dd1fe 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 0000000..72c2ea9 --- /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 -- GitLab