Skip to content

Commit

Permalink
🧪 测试失败
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkioTomas committed Jul 16, 2024
1 parent a23f8dd commit e26fe76
Show file tree
Hide file tree
Showing 7 changed files with 1,139 additions and 81 deletions.
12 changes: 12 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
// 其他 Jest 配置选项...
transform: {
'^.+\\.(js|jsx)$': 'babel-jest',
},
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
"build": "jest && webpack --mode production"
},
"devDependencies": {
"@babel/core": "^7.24.9",
"@babel/preset-env": "^7.24.8",
"@eslint/eslintrc": "^3.0.2",
"@eslint/js": "^9.0.0",
"@rollup/pluginutils": "^5.1.0",
"babel-jest": "^29.7.0",
"crypto": "^1.0.1",
"eslint": "^7.32.0 || ^8.2.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -25,8 +28,6 @@
"jest": "^29.7.0",
"prettier": "3.2.5",
"rollup": "^4.18.1",
"rollup-plugin-alias": "^2.2.0",
"rollup-plugin-concat": "^1.0.3",
"rollup-plugin-multi-entry": "^2.1.0",
"rollup-terser": "^1.0.3"
},
Expand Down
50 changes: 30 additions & 20 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,43 @@ fs.writeFileSync(
path.join('dist', 'rules.json'),
JSON.stringify(rules, null, 2),
);

let moduleName = 'common';
outputs.push({
input: path.join('src', 'utils', 'index.js'),
output: {
file: path.join('dist', 'common.js'), // 输出文件路径
file: path.join('dist', `${moduleName}.js`), // 输出文件路径
format: 'iife', // 输出格式为 IIFE
name: 'common', // 全局变量名
name: moduleName, // 全局变量名
footer: `
let BillType = common.BillType;
let Currency = common.Currency;
let DataType = common.DataType;
let RuleObject = common.RuleObject;
let findNonEmptyString = common.findNonEmptyString;
let formatDate = common.formatDate;
let isTimeInRange = common.isTimeInRange;
let stripHtml = common.stripHtml;
let toDoubleFloat = common.toDoubleFloat;
let toFloat = common.toFloat;
let BillType = ${moduleName}.BillType;
let Currency = ${moduleName}.Currency;
let DataType = ${moduleName}.DataType;
let RuleObject = ${moduleName}.RuleObject;
let Html = {
findNonEmptyString: ${moduleName}.findNonEmptyString,
stripHtml: ${moduleName}.stripHtml,
};
let Number = {
toDoubleFloat: ${moduleName}.toDoubleFloat,
toFloat: ${moduleName}.toFloat,
};
let Time ={
formatDate: ${moduleName}.formatDate,
isTimeInRange: ${moduleName}.isTimeInRange,
};
`,
},
plugins: [
terser(),
{
name: 'process-code', // 插件名称
},
],
//external: id => externalFilter(id),
plugins: [terser()],
});

outputs.push({
input: path.join('src', 'category', 'main.js'),
output: {
file: path.join('dist', `category.js`), // 输出文件路径
format: 'iife', // 输出格式为 IIFE
name: 'category', // 全局变量名
},
plugins: [terser()],
});

export default outputs;
16 changes: 8 additions & 8 deletions src/tests/TestUtils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const fs = require("fs");
const path = require("path");
const fs = require('fs');
const path = require('path');

const crypto = require("crypto");
const crypto = require('crypto');
function calculateMD5(input) {
const hash = crypto.createHash("md5");
const hash = crypto.createHash('md5');
hash.update(input);
return hash.digest("hex");
return hash.digest('hex');
}

/**
Expand All @@ -14,8 +14,8 @@ function calculateMD5(input) {
* @returns {string}
*/
function getTestFile(name) {
const dataFilePath = path.join(dirname, "tests", `${name}.txt`);
return fs.readFileSync(dataFilePath, "utf8");
const dataFilePath = path.join(dirname, 'tests', `${name}.txt`);
return fs.readFileSync(dataFilePath, 'utf8');
}

let get = null;
Expand Down Expand Up @@ -51,7 +51,7 @@ export function testAnkio(name, results) {
app: app,
};
//判断dist文件夹是否不存在,不存在就创建
const dist = path.join(__dirname, "..", "..", "dist");
const dist = path.join(__dirname, '..', '..', 'tests');
if (!fs.existsSync(dist)) {
fs.mkdirSync(dist);
}
Expand Down
68 changes: 49 additions & 19 deletions src/tests/appRule.test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
import { DataType } from "../utils/DataType";
import path from "path";
import fs from "fs";
import { DataType } from '../utils/DataType';
import path from 'path';
import fs from 'fs';

const { execSync } = require("child_process");
const { execSync } = require('child_process');

execSync("yarn dev", { stdio: "inherit" });
execSync('yarn dev', { stdio: 'inherit' });

const distDirPath = path.join(__dirname, "..", "..", "dist");
const distDirPath = path.join(__dirname, '..', '..', 'dist');

const dataFilePath = path.join(distDirPath, "rule.js");
const dataFilePath = path.join(distDirPath, 'rule.js');
// 使用readFileSync来同步读取文件内容
const ruleJS = fs.readFileSync(dataFilePath, "utf8");
// 读取common.js文件内容,作为文件头
let js = fs.readFileSync(path.join(distDirPath, 'common.js'), 'utf8');

// 读取rules.json文件内容
// {
// "ruleName": "rule_25",
// "ruleChineseName": "微信转账",
// "ruleApp": "com.tencent.mm",
// "ruleType": "app",
// "path": "app/com.tencent.mm/微信转账.js"
// }
// ]

const rules = JSON.parse(
fs.readFileSync(path.join(distDirPath, 'rules.json'), 'utf8'),
);

for (const rule of rules) {
js += fs.readFileSync(path.join(distDirPath, rule.path), 'utf8');
}

js += `
window.rules = [
`;
for (const rule of rules) {
js += `{name:"${rule.ruleChineseName}",obj:${rule.ruleName}},`;
}
js += `
];
`;
const tests = path.join(__dirname, '..', '..', 'tests');

// 读取dist目录下的所有文件名
const fileNames = fs.readdirSync(distDirPath);
const fileNames = fs.readdirSync(tests);

// 过滤出所有的.json文件名
const jsonFileNames = fileNames.filter(
(fileName) => path.extname(fileName) === ".json",
fileName => path.extname(fileName) === '.json',
);

// 读取每个.json文件的内容
const jsonFilesContent = jsonFileNames.map((jsonFileName) => {
const jsonFilePath = path.join(distDirPath, jsonFileName);
const jsonFileContent = fs.readFileSync(jsonFilePath, "utf8");
const jsonFilesContent = jsonFileNames.map(jsonFileName => {
const jsonFilePath = path.join(tests, jsonFileName);
const jsonFileContent = fs.readFileSync(jsonFilePath, 'utf8');
return JSON.parse(jsonFileContent);
});

test("App规则调用校验", () => {
test('App规则调用校验', () => {
//读取dist目录下所有的json
jsonFilesContent.forEach((jsonFileContent) => {
jsonFilesContent.forEach(jsonFileContent => {
const { name, results, datas, type, app } = jsonFileContent;
for (let i = 0; i < results.length; i++) {
const result = results[i];
const data = datas[i];
var window = {
data: data,
dataType: type,
app: app,
rules: [],
};
function print(callbackResult) {
expect(callbackResult).toEqual(result);
}
try {
eval(ruleJS);
// console.log(js);
eval(js);
} catch (e) {
console.error("Failed: " + name + " ", e);
console.error('Failed: ' + name + ' ', e);
}
}
});
Expand Down
Loading

0 comments on commit e26fe76

Please sign in to comment.