Skip to content

Commit

Permalink
feat: adding create auction to index file
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Jul 11, 2024
1 parent ef516bd commit cf53f4d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 89 deletions.
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@
"main": "src/index.ts",
"author": "Márcio Barbosa <[email protected]>",
"license": "UNLICENSED",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.js"
}
},
"files": ["dist/**/*.js", "dist/**/*.d.ts", "package.json", "README.md"],
"scripts": {
"build": "bun build",
"build": "bun build src/index.ts --outdir=dist",
"test": "bun test",
"doctest": "bun run src/lib/doctest.test.ts",
"format": "biome check",
"format:fix": "biome check --write"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
"@supabase/doctest-js": "^0.1.0",
"@types/bun": "^1.1.6",
"husky": "^9.0.11",
"msw": "^2.3.1"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./functions/report-event";
export * from "./functions/create-auction";
export * from "./interfaces/events.interface";
153 changes: 66 additions & 87 deletions src/tests/doctest.testx.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,78 @@
import { afterAll, afterEach, beforeAll, describe, expect, it } from "bun:test";
import * as path from "node:path";
import { setupServer } from "msw/node";
import { apis, baseURL } from "../constants/apis.constant";
import { handlers } from "../constants/handlers.constant";
import { reportEvent } from "../functions/report-event";
import APIClient from "../lib/api-client";
import { generateTestCases } from "../lib/generate-test-cases";
// import * as path from "node:path";
// import { describe, it, expect, beforeAll, afterAll, afterEach } from "bun:test";
// import { generateTestCases } from "../lib/generate-test-cases";
// import { reportEvent } from "../functions/report-event";
// import { apis, baseURL } from "../constants/apis.constant";
// import APIClient from "../lib/api-client";
// import { setupServer } from "msw/node";
// import { handlers } from "../constants/handlers.constant";

const server = setupServer(handlers.test);
const filePath = path.resolve(__dirname, "../functions/report-event.ts");
// const server = setupServer(handlers.test);
// const filePath = path.resolve(__dirname, "../functions/report-event.ts");

interface TestCaseInterface {
code: string;
expected: unknown;
}
// interface TestCaseInterface {
// code: string;
// expected: unknown;
// }

let testCases: TestCaseInterface[] = [];
// let testCases: TestCaseInterface[] = [];

beforeAll(async () => {
server.listen();
testCases = await generateTestCases(filePath);
console.log("Test cases:", testCases);
});
// beforeAll(async () => {
// server.listen();
// testCases = await generateTestCases(filePath);
// console.log("Test cases:", testCases);
// });

afterEach(() => server.resetHandlers());
afterAll(() => server.close());
// afterEach(() => server.resetHandlers());
// afterAll(() => server.close());

describe("doctest", () => {
console.log("Test cases:", testCases);
testCases = [
{
code: 'const event = { eventType: "test", eventData: {} };\nconst config = { token: "my-token" };\nconst result = await reportEvent(event, config);\n // { "ok": true, "retry": false }',
expected: {
ok: true,
retry: false,
},
},
];
// describe("doctest", () => {
// console.log("Test cases:", testCases);
// testCases = [
// {
// code: "const event = { eventType: \"test\", eventData: {} };\nconst config = { token: \"my-token\" };\nconst result = await reportEvent(event, config);\n // { \"ok\": true, \"retry\": false }",
// expected: {
// ok: true,
// retry: false
// }
// }
// ]

testCases.forEach((testCase, index) => {
it(`example ${index + 1}`, async () => {
const code = `
(async () => {
const event = { eventType: "test", eventData: {} };
const config = { token: "my-token", apiKey: "test-api-key" };
const result = await reportEvent(event, config);
return result;
})();
`;
// testCases.forEach((testCase, index) => {
// it(`example ${index + 1}`, async () => {
// const code = `
// (async () => {
// const event = { eventType: "test", eventData: {} };
// const config = { token: "my-token", apiKey: "test-api-key" };
// const result = await reportEvent(event, config);
// return result;
// })();
// `;

const func = new Function(
"require",
"exports",
"module",
"__filename",
"__dirname",
"reportEvent",
"apis",
"baseURL",
"APIClient",
code,
);
const exports: unknown = {};
const module = { exports };
// const func = new Function("require", "exports", "module", "__filename", "__dirname", "reportEvent", "apis", "baseURL", "APIClient", code);
// const exports: unknown = {};
// const module = { exports };

// Mock require function
const customRequire = (moduleName: string): unknown => {
if (moduleName === "../functions/report-event") {
return { reportEvent };
}
if (moduleName === "../constants/apis.constant") {
return { apis, baseURL };
}
if (moduleName === "../lib/api-client") {
return APIClient;
}
throw new Error(`Module not found: ${moduleName}`);
};
// // Mock require function
// const customRequire = (moduleName: string): unknown => {
// if (moduleName === "../functions/report-event") {
// return { reportEvent };
// }
// if (moduleName === "../constants/apis.constant") {
// return { apis, baseURL };
// }
// if (moduleName === "../lib/api-client") {
// return APIClient;
// }
// throw new Error(`Module not found: ${moduleName}`);
// };

const result = await func(
customRequire,
exports,
module,
__filename,
__dirname,
reportEvent,
apis,
baseURL,
APIClient,
);
// const result = await func(customRequire, exports, module, __filename, __dirname, reportEvent, apis, baseURL, APIClient);

console.log("Result:", result); // Debugging line
// console.log("Result:", result); // Debugging line

expect(result).toBeDefined();
expect(result).toEqual(testCase.expected);
});
});
});
// expect(result).toBeDefined();
// expect(result).toEqual(testCase.expected);
// });
// });
// });

0 comments on commit cf53f4d

Please sign in to comment.