Skip to content

Commit b3d0323

Browse files
authored
feat: Add support for CORS requests (#14)
* fix: Ensure response object has non-empty url property * feat: Add CORS support * Fix test * Unformat mdx files
1 parent 541a6bc commit b3d0323

27 files changed

+2222
-866
lines changed

.github/ISSUE_TEMPLATE/bug-report.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ body:
1313
value: |
1414
Node version:
1515
npm version:
16-
Mentoss version:
16+
Mentoss version:
1717
validations:
1818
required: true
1919
- type: textarea

.github/ISSUE_TEMPLATE/docs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ body:
2222
attributes:
2323
label: What do you think is the correct solution?
2424
description: |
25-
Please explain how you'd like to change the ESLint docs to address the problem.
25+
Please explain how you'd like to change the Mentoss docs to address the problem.
2626
validations:
2727
required: true
2828
- type: checkboxes

.github/workflows/release-please.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ jobs:
4747

4848
- name: Push to latest and version branches branch
4949
run: |
50-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
51-
git config --global user.name "github-actions[bot]"
52-
git push -f origin main:v${{ steps.release.outputs.major }}.x
53-
git push -f origin main:latest
50+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
51+
git config --global user.name "github-actions[bot]"
52+
git push -f origin main:v${{ steps.release.outputs.major }}.x
53+
git push -f origin main:latest
5454
if: ${{ steps.release.outputs.release_created }}
55-
55+
5656
# Tweets out release announcement
5757
- run: 'node dist/esm/bin.js -t -b -m "${{ github.event.repository.full_name }} v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}.${{ steps.release.outputs.patch }} has been released!\n\n${{ github.event.repository.html_url }}/releases/tag/${{ steps.release.outputs.tag_name }}"'
5858
if: ${{ steps.release.outputs.release_created }}

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ dist
22
CHANGELOG.md
33
jsr.json
44
.prettierignore
5+
**/*.mdx

docs/astro.config.mjs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
// @ts-check
2-
import { defineConfig } from 'astro/config';
3-
import starlight from '@astrojs/starlight';
2+
import { defineConfig } from "astro/config";
3+
import starlight from "@astrojs/starlight";
44

55
// https://astro.build/config
66
export default defineConfig({
77
integrations: [
88
starlight({
9-
title: 'Mentoss',
9+
title: "Mentoss",
1010
social: {
11-
github: 'https://github.com/humanwhocodes/mentoss',
11+
github: "https://github.com/humanwhocodes/mentoss",
1212
},
1313
editLink: {
14-
baseUrl: 'https://github.com/humanwhocodes/mentoss/edit/main/docs/',
14+
baseUrl:
15+
"https://github.com/humanwhocodes/mentoss/edit/main/docs/",
1516
},
1617
lastUpdated: true,
1718
sidebar: [
1819
{
19-
label: 'Getting Started',
20-
slug: 'getting-started',
20+
label: "Getting Started",
21+
slug: "getting-started",
2122
},
2223
{
23-
label: 'Mock Servers',
24-
autogenerate: { directory: 'mock-servers' },
24+
label: "Mock Servers",
25+
autogenerate: { directory: "mock-servers" },
2526
},
2627
{
27-
label: 'Fetch Mockers',
28-
autogenerate: { directory: 'fetch-mockers' },
28+
label: "Fetch Mockers",
29+
autogenerate: { directory: "fetch-mockers" },
2930
},
3031
],
3132
components: {
32-
Footer: './src/components/MyFooter.astro',
33-
}
33+
Footer: "./src/components/MyFooter.astro",
34+
},
3435
}),
3536
],
3637
});

docs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
"astro": "^5.1.5",
1515
"sharp": "^0.32.5"
1616
}
17-
}
17+
}

docs/src/content.config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { defineCollection } from 'astro:content';
2-
import { docsLoader } from '@astrojs/starlight/loaders';
3-
import { docsSchema } from '@astrojs/starlight/schema';
1+
import { defineCollection } from "astro:content";
2+
import { docsLoader } from "@astrojs/starlight/loaders";
3+
import { docsSchema } from "@astrojs/starlight/schema";
44

55
export const collections = {
66
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),

docs/src/content/docs/fetch-mockers/creating-fetch-mocker.mdx

+60-62
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ title: Creating a Fetch Mocker
33
description: Learn how to create a fetch mocker to mock the fetch() function
44
---
55

6-
A *fetch mocker* in Mentoss is how you create a mocked version of a `fetch()` function. Fetch mockers are used to intercept calls to `fetch()` and return responses that you've defined in your mock servers. This allows you to test your code without making real network requests.
6+
A _fetch mocker_ in Mentoss is how you create a mocked version of a `fetch()` function. Fetch mockers are used to intercept calls to `fetch()` and return responses that you've defined in your mock servers. This allows you to test your code without making real network requests.
77

88
## Create a new `FetchMocker` instance
99

1010
To get started, import the `FetchMocker` class and create a new instance. The only argument is an object with the following properties:
1111

12-
* `servers` (required) - an array of `MockServer` instances to use for mocking fetch requests
12+
- `servers` (required) - an array of `MockServer` instances to use for mocking fetch requests
1313

1414
Here's an example:
1515

@@ -19,7 +19,7 @@ import { MockServer, FetchMocker } from "mentoss";
1919
const server = new MockServer("https://api.example.com");
2020

2121
const mocker = new FetchMocker({
22-
servers: [server]
22+
servers: [server],
2323
});
2424
```
2525

@@ -36,35 +36,34 @@ There are two ways to use a mocked `fetch()` function:
3636

3737
Each `FetchMocker` instance creates a `fetch()` function that can be extracted and used in place of the global `fetch()` function. Here's an example:
3838

39-
```js {10-11, 23-24}
39+
```js {10-11, 22-23}
4040
import { MockServer, FetchMocker } from "mentoss";
4141
import { expect } from "chai";
4242

4343
describe("My API", () => {
44-
const server = new MockServer("https://api.example.com");
45-
const mocker = new FetchMocker({
46-
servers: [server]
47-
});
48-
49-
// extract the fetch function
50-
const myFetch = mocker.fetch;
51-
52-
// reset the server after each test
53-
afterEach(() => {
54-
server.clear();
55-
});
56-
57-
it("should return a 200 status code", async () => {
58-
59-
// set up the route to test
60-
server.get("/ping", 200);
61-
62-
// make the request
63-
const response = await myFetch("https://api.example.com/ping");
64-
65-
// check the response
66-
expect(response.status).to.equal(200);
67-
});
44+
const server = new MockServer("https://api.example.com");
45+
const mocker = new FetchMocker({
46+
servers: [server],
47+
});
48+
49+
// extract the fetch function
50+
const myFetch = mocker.fetch;
51+
52+
// reset the server after each test
53+
afterEach(() => {
54+
mocker.clearAll();
55+
});
56+
57+
it("should return a 200 status code", async () => {
58+
// set up the route to test
59+
server.get("/ping", 200);
60+
61+
// make the request
62+
const response = await myFetch("https://api.example.com/ping");
63+
64+
// check the response
65+
expect(response.status).to.equal(200);
66+
});
6867
});
6968
```
7069

@@ -74,44 +73,43 @@ In this example, the `fetch()` function is extracted from the `mocker` instance
7473

7574
If you want to mock the global `fetch()` function, you can use the `mockGlobal()` and `unmockGlobal()` methods provided by the fetch mocker. Here's an example:
7675

77-
```js {12-15, 22-25, 32-33}
76+
```js {12-15, 22-25, 31-32}
7877
import { MockServer, FetchMocker } from "mentoss";
7978
import { expect } from "chai";
8079

8180
describe("My API", () => {
82-
let mocker;
83-
84-
const server = new MockServer("https://api.example.com");
85-
mocker = new FetchMocker({
86-
servers: [server]
87-
});
88-
89-
// mock the global fetch function
90-
before(() => {
91-
mocker.mockGlobal();
92-
});
93-
94-
// reset the server after each test
95-
afterEach(() => {
96-
server.clear();
97-
});
98-
99-
// unmock the global fetch function
100-
after(() => {
101-
mocker.unmockGlobal();
102-
});
103-
104-
it("should return a 200 status code", async () => {
105-
106-
// set up the route to test
107-
server.get("/ping", 200);
108-
109-
// make the request
110-
const response = await fetch("https://api.example.com/ping");
111-
112-
// check the response
113-
expect(response.status).to.equal(200);
114-
});
81+
let mocker;
82+
83+
const server = new MockServer("https://api.example.com");
84+
mocker = new FetchMocker({
85+
servers: [server],
86+
});
87+
88+
// mock the global fetch function
89+
before(() => {
90+
mocker.mockGlobal();
91+
});
92+
93+
// reset the server after each test
94+
afterEach(() => {
95+
mocker.clearAll();
96+
});
97+
98+
// unmock the global fetch function
99+
after(() => {
100+
mocker.unmockGlobal();
101+
});
102+
103+
it("should return a 200 status code", async () => {
104+
// set up the route to test
105+
server.get("/ping", 200);
106+
107+
// make the request
108+
const response = await fetch("https://api.example.com/ping");
109+
110+
// check the response
111+
expect(response.status).to.equal(200);
112+
});
115113
});
116114
```
117115

0 commit comments

Comments
 (0)