Skip to content

Commit

Permalink
test(io): add Jest tests for io package
Browse files Browse the repository at this point in the history
  • Loading branch information
anran758 committed Mar 18, 2024
1 parent d7a3210 commit ebfb029
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 318 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libs/**/lib
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"prettier": "^2.2.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
},
"dependencies": {
"jest": "^29.7.0"
}
}
3 changes: 1 addition & 2 deletions packages/faas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
},
"devDependencies": {
"@types/lodash": "^4.14.202",
"@mincloudx/types": "workspace:*",
"jest": "^29.7.0"
"@mincloudx/types": "workspace:*"
},
"dependencies": {
"lodash": "^4.17.21"
Expand Down
3 changes: 2 additions & 1 deletion packages/faas/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
"exclude": ["**/*.test.ts"],
"compilerOptions": {
"typeRoots": ["../../node_modules/@types", "./types"],
"rootDir": "./src",
"noImplicitAny": false,
"lib": ["ESNext"],
"target": "es2022",
"module": "NodeNext",
"moduleResolution": "nodenext",
"outDir": "lib/esm",
"declarationDir": "lib/esm/types",
"rootDir": ".",
"baseUrl": "./"
}
}
35 changes: 35 additions & 0 deletions packages/io/__tests__/io.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 在你的测试文件的顶部,模拟 @mincloudx/baas 包
// jest.mock('@mincloudx/baas', () => ({
// TableObject: jest.fn().mockImplementation(tableName => ({
// create: jest.fn(),
// get: jest.fn(),
// update: jest.fn(),
// delete: jest.fn(),
// })),
// }));

// 引入所需模块和初始化代码
require('../tool/baas-init');
const { createIo } = require('..');
const TABLE_OPERATION_KEYS = ['create', 'get', 'update', 'delete'];

describe('createIo Functionality', () => {
it('register table to io', async () => {
const tables = ['channel', 'product'];
const io = createIo({ tables });

expect(io).toHaveProperty('channel');
TABLE_OPERATION_KEYS.forEach(key => {
expect(io.channel).toHaveProperty(key);
});
});

it('io.channel should return an instance of BaaS.TableObject', () => {
const tables = ['channel'];
const io = createIo({ tables });

expect(io.channel).toBeDefined();
expect(io.channel.table).toBeDefined();
expect(io.channel.table._tableID).toEqual('channel');
});
});
2 changes: 1 addition & 1 deletion packages/io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"scripts": {
"lint": "eslint .",
"test": "nx exec -- ts-node ./test/index.ts",
"test": "jest --verbose",
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
Expand Down
4 changes: 2 additions & 2 deletions packages/io/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type RecordId = string | number;

export interface Operation {
readonly table: any;
create: <T = Record<any, any>>(
create: <T extends object = Record<any, any>>(
data: T,
options?: BasicOperationOptions,
) => Promise<Record<any, any>>;
update: <T = Record<any, any>>(
update: <T extends object = Record<any, any>>(
id: RecordId,
data: T,
options?: BasicOperationOptions,
Expand Down
9 changes: 0 additions & 9 deletions packages/io/test/index.ts

This file was deleted.

File renamed without changes.
19 changes: 12 additions & 7 deletions packages/io/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"extends": "../../tsconfig.json",
"include": ["src", "test", "types", "tool/baas-init.ts"],
"exclude": ["**/*.test.ts"],
"compilerOptions": {
"typeRoots": [
"../../node_modules/@types",
"./node_modules/@types",
"./types"
],
"allowJs": true,
"noImplicitAny": false,
"target": "ES2019",
"module": "commonJS",
"moduleResolution": "node",
"lib": ["es2015", "dom"]
},
"include": ["src", "test", "types"]
"target": "es2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["es2015"],
"declarationDir": "lib/types",
"outDir": "lib",
"rootDir": ".",
"baseUrl": "./src"
}
}
70 changes: 29 additions & 41 deletions packages/io/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
// Generated using webpack-cli https://github.com/webpack/webpack-cli

const path = require('path');

const isProduction = process.env.NODE_ENV == 'production';

const config = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
// 使 Node.js 与 browser 均能使用
libraryTarget: 'umd',
globalObject: 'this',
},
plugins: [
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
},

// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
},
resolve: {
extensions: ['.tsx', '.ts', '.jsx', '.js', '...'],
},
};

module.exports = () => {
if (isProduction) {
config.mode = 'production';
} else {
config.mode = 'development';
}
return config;
return {
mode: isProduction ? 'production' : 'development',
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'umd',
globalObject: 'this',
},
plugins: [
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
loader: 'ts-loader',
exclude: ['/node_modules/'],
},
],
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
extensions: ['.ts', '.js'],
fallback: { path: false },
},
};
};
Loading

0 comments on commit ebfb029

Please sign in to comment.