diff --git a/package.json b/package.json index 213887ac..37257593 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "types:check": "tsc --project ./jsconfig.json", "types:lint": "dtslint --expectOnly packages/fetch-mock/types", "prepare": "husky || echo \"husky not available\"", - "build": "rollup -c", + "build": "npm run build -w=packages/fetch-mock -w=packages/core", "docs": "npm run start -w docs", "test:ci": "vitest .", "test:legacy": "vitest ./packages/fetch-mock/test/specs", diff --git a/packages/ARCHITECTURE.md b/packages/ARCHITECTURE.md deleted file mode 100644 index 51ece78c..00000000 --- a/packages/ARCHITECTURE.md +++ /dev/null @@ -1,91 +0,0 @@ -# Goals - -Completely separate the core behaviour from behaviours that other test libraries may have their own ideas about so that -1. APIs don't have any hard conflicts -2. Within a given ecosystem there is one way to do something, idiomatic to that ecosystem -3. When a new flavour of the month testing library comes along, it's easy to add idiomatic support - -# Modules - -## fetch handler -- orchestrates building a response and sending it -- Needs to be provided with a router instance -- Puts all details of each call in a CallHistory instance if provided, including which route handled it - -## Response builder - -## Router -- has a submodule - Route -- given a request finds (if it can) a matching route -- Should provide some debugging info - -## CallHistory -- records all fetch calls and provides low level APIs for inspecting them -- API for matching calls should - with the exceotion of respecting route names - behave identically to the router. -- Shodl provide some debugging info - -## FetchMock -- Wraps fetch handler, router and inspection together -- Allows creating instances -- Allows setting options - -- DOES NOT DO ANY ACTUAL MOCKING!!! Or maybe there is a very explicit .mockGlobal() method (or ios this in @fetch-mock/standalone?) - - -FetchMock.createInstance = function () { - const instance = Object.create(FetchMock); - instance.router = this.router.clone() - instance.calls = this.calls.clone() - return instance; -}; - -- Where do spy() and pass() live? TBD -- Note that sandbox is gone - complex to implement and a source of many clashes with other testing libraries -## @fetch-mock/standalone, @fetch-mock/jest, @fetch-mock/vitest, ... - -Wrappers that act as plugins for the testing libraries' own mocking, inspection and lifecycle management APIs - -API split - -FetchMock -- config -- createInstance -- bindMethods -- getOption -- flush - -FetchHandler -- extractBodyThenHandle -- fetchHandler -- generateResponse -- statusTextMap (but probably doesn't need to be public anyway) - -Router -- needsAsyncBodyExtraction -- execute -- addMatcher -- done -- add/route -- get/post/... -- catch -- compileRoute - -CallHistory -- recordCall -- filterCalls (private) -- calls -- lastCall - -Standalone wrapper -- getNativeFetch -- called -- lastUrl -- lastOptions -- lastResponse -- mockGlobal -- spy?? -- pass?? -- resetBehavior -- resetHistory -- restore -- reset \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index be99ceef..f4f5e7f0 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,12 +1,18 @@ { "name": "@fetch-mock/core", "description": "Utility for creating mock fetch implementation", - "exports": "src/index.js", "version": "0.4.0", - "main": "index.js", - "types": "types/index.d.ts", + "main": "./dist/commonjs.js", + "module": "./src/index.js", + "exports": { + "types": "./types/index.d.ts", + "browser": "./src/index.js", + "import": "./src/index.js", + "require": "./dist/commonjs.js" + }, + "types": "./types/index.d.ts", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "build": "rollup -c" }, "repository": { "type": "git", diff --git a/packages/core/rollup.config.mjs b/packages/core/rollup.config.mjs new file mode 100644 index 00000000..8a8bf783 --- /dev/null +++ b/packages/core/rollup.config.mjs @@ -0,0 +1,18 @@ +import { nodeResolve } from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; +import { writeFile, mkdir } from 'fs/promises'; +export default { + input: './src/index.js', + output: { + dir: './dist', + entryFileNames: 'commonjs.js', + format: 'commonjs', + }, + plugins: [ + nodeResolve({ preferBuiltins: false }), + commonjs(), + // sourcemaps(), + // builtins(), + // globals(), + ], +}; diff --git a/packages/core/src/Route.js b/packages/core/src/Route.js index bd83e925..8980e33e 100644 --- a/packages/core/src/Route.js +++ b/packages/core/src/Route.js @@ -1,6 +1,6 @@ //@type-check -import { builtInMatchers } from './Matchers'; -import statusTextMap from './StatusTextMap'; +import { builtInMatchers } from './Matchers.js'; +import statusTextMap from './StatusTextMap.js'; /** @typedef {import('./Matchers').RouteMatcher} RouteMatcher */ /** @typedef {import('./CallHistory').CallLog} CallLog */ diff --git a/packages/core/src/package.json b/packages/core/src/package.json new file mode 100644 index 00000000..1632c2c4 --- /dev/null +++ b/packages/core/src/package.json @@ -0,0 +1 @@ +{"type": "module"} \ No newline at end of file diff --git a/packages/README.md b/packages/core/types/FetchHandler.d.ts similarity index 100% rename from packages/README.md rename to packages/core/types/FetchHandler.d.ts diff --git a/packages/fetch-mock/package.json b/packages/fetch-mock/package.json index def74d83..a959a400 100644 --- a/packages/fetch-mock/package.json +++ b/packages/fetch-mock/package.json @@ -11,6 +11,9 @@ "require": "./dist/commonjs.js" }, "types": "./types/index.d.ts", + "scripts": { + "build": "rollup -c" + }, "repository": { "type": "git", "url": "git+https://github.com/wheresrhys/fetch-mock.git", diff --git a/packages/fetch-mock/rollup.config.mjs b/packages/fetch-mock/rollup.config.mjs new file mode 100644 index 00000000..9d682656 --- /dev/null +++ b/packages/fetch-mock/rollup.config.mjs @@ -0,0 +1,18 @@ +import { nodeResolve } from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; +import { writeFile, mkdir } from 'fs/promises'; +export default { + input: './src/index.js', + output: { + dir: './dist', + entryFileNames: 'commonjs.js', + format: 'commonjs', + }, + plugins: [ + nodeResolve({ preferBuiltins: false }), + commonjs(), + // sourcemaps(), + // builtins(), + // globals(), + ], +}; diff --git a/rollup.config.js b/rollup.config.js deleted file mode 100644 index 67057830..00000000 --- a/rollup.config.js +++ /dev/null @@ -1,35 +0,0 @@ -import { nodeResolve } from '@rollup/plugin-node-resolve'; -import commonjs from '@rollup/plugin-commonjs'; -import { writeFile, mkdir } from 'fs/promises'; -function createCommonJsPackage() { - const pkg = { type: 'commonjs' }; - - return { - name: 'cjs-package', - buildEnd: async () => { - await mkdir('./packages/fetch-mock/dist', { recursive: true }); - await writeFile( - './packages/fetch-mock/dist/package.json', - JSON.stringify(pkg, null, 2), - ); - }, - }; -} - -export default { - input: 'packages/fetch-mock/src/index.js', - output: { - dir: 'packages/fetch-mock/dist', - entryFileNames: 'commonjs.js', - format: 'commonjs', - }, - plugins: [ - nodeResolve({ preferBuiltins: false }), - // resolve({ preferBuiltins: true }), - commonjs(), - createCommonJsPackage(), - // sourcemaps(), - // builtins(), - // globals(), - ], -};