@labdigital/commercetools-mock 2.4.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -8
- package/dist/index.cjs +43 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/ctMock.ts +59 -20
- package/src/services/custom-object.test.ts +17 -1
- package/src/services/customer.test.ts +20 -0
package/README.md
CHANGED
|
@@ -3,9 +3,16 @@
|
|
|
3
3
|
[<img src="https://img.shields.io/npm/v/@labdigital/commercetools-mock">](https://www.npmjs.com/package/@labdigital/commercetools-mock)
|
|
4
4
|
[](https://codecov.io/gh/labd/commercetools-node-mock)
|
|
5
5
|
|
|
6
|
-
This library mocks the Commercetools rest
|
|
7
|
-
codebases interacting with the commercetools api. It uses the same proven
|
|
8
|
-
as our testing module in the
|
|
6
|
+
This library mocks the Commercetools rest API to ease testing of your typescript
|
|
7
|
+
codebases interacting with the commercetools api. It uses the same proven
|
|
8
|
+
approach as our testing module in the
|
|
9
|
+
[commercetools Python SDK](https://github.com/labd/commercetools-python-sdk/tree/main/src/commercetools/testing).
|
|
10
|
+
|
|
11
|
+
Since version 2 of this library it is based on [msw](https://mswjs.io/) instead
|
|
12
|
+
of nock. It is now therefore als recommended to manage the msw server yourself
|
|
13
|
+
and use the `registerHandlers` method to register the handlers on this server.
|
|
14
|
+
|
|
15
|
+
This allows you to use the same server for mocking other API's as well.
|
|
9
16
|
|
|
10
17
|
## Installation
|
|
11
18
|
|
|
@@ -15,13 +22,15 @@ yarn add --dev @labdigital/commercetools-mock
|
|
|
15
22
|
|
|
16
23
|
## Docker image
|
|
17
24
|
|
|
18
|
-
This codebase is also available as a docker image where it provides a runnable
|
|
19
|
-
|
|
25
|
+
This codebase is also available as a docker image where it provides a runnable
|
|
26
|
+
http server exposing the mocked endpoints. See
|
|
27
|
+
https://hub.docker.com/r/labdigital/commercetools-mock-server
|
|
20
28
|
|
|
21
29
|
## Example
|
|
22
30
|
|
|
23
31
|
```typescript
|
|
24
32
|
import { CommercetoolsMock, getBaseResourceProperties } from '@labdigital/commercetools-mock'
|
|
33
|
+
import { setupServer } from 'msw/node'
|
|
25
34
|
|
|
26
35
|
const ctMock = new CommercetoolsMock({
|
|
27
36
|
apiHost: 'https://localhost',
|
|
@@ -34,7 +43,8 @@ const ctMock = new CommercetoolsMock({
|
|
|
34
43
|
|
|
35
44
|
describe('A module', () => {
|
|
36
45
|
beforeAll(() => {
|
|
37
|
-
|
|
46
|
+
const server = setupServer()
|
|
47
|
+
ctMock.registerHandlers(server)
|
|
38
48
|
|
|
39
49
|
ctMock.project().add('type', {
|
|
40
50
|
...getBaseResourceProperties()
|
|
@@ -44,6 +54,7 @@ describe('A module', () => {
|
|
|
44
54
|
})
|
|
45
55
|
|
|
46
56
|
afterAll(() => {
|
|
57
|
+
server.clearHandlers()
|
|
47
58
|
ctMock.stop()
|
|
48
59
|
})
|
|
49
60
|
|
|
@@ -61,7 +72,9 @@ describe('A module', () => {
|
|
|
61
72
|
})
|
|
62
73
|
```
|
|
63
74
|
|
|
64
|
-
##
|
|
75
|
+
## Contributing
|
|
76
|
+
|
|
77
|
+
### Adding a new service
|
|
65
78
|
|
|
66
79
|
Implement the following:
|
|
67
80
|
|
|
@@ -71,7 +84,7 @@ Implement the following:
|
|
|
71
84
|
- Add new service to src/storage.ts InMemoryStorage
|
|
72
85
|
- Adjust src/types.ts RepositoryMap and possibly serviceTypes
|
|
73
86
|
|
|
74
|
-
|
|
87
|
+
### Releasing
|
|
75
88
|
|
|
76
89
|
This codebases use [@changesets](https://github.com/changesets/changesets) for release and version management
|
|
77
90
|
|
package/dist/index.cjs
CHANGED
|
@@ -6992,32 +6992,45 @@ var CommercetoolsMock = class {
|
|
|
6992
6992
|
});
|
|
6993
6993
|
return app;
|
|
6994
6994
|
}
|
|
6995
|
-
|
|
6996
|
-
|
|
6997
|
-
|
|
6998
|
-
|
|
6999
|
-
|
|
7000
|
-
|
|
7001
|
-
_globalListeners.forEach((listener) => listener.close());
|
|
7002
|
-
}
|
|
7003
|
-
}
|
|
7004
|
-
const server = this.app;
|
|
7005
|
-
this._mswServer = (0, import_node.setupServer)(
|
|
6995
|
+
// registerHandlers is an alternative way to work with commercetools-mock, it
|
|
6996
|
+
// allows you to manage msw server yourself and register the handlers needed
|
|
6997
|
+
// for commercetools-mock to work.
|
|
6998
|
+
registerHandlers(server) {
|
|
6999
|
+
const app = this.app;
|
|
7000
|
+
server.use(
|
|
7006
7001
|
import_msw.http.post(`${this.options.authHost}/oauth/*`, async ({ request }) => {
|
|
7007
7002
|
const body = await request.text();
|
|
7008
7003
|
const url = new URL(request.url);
|
|
7009
7004
|
const headers = copyHeaders(request.headers);
|
|
7010
|
-
const res = await (0, import_light_my_request.default)(
|
|
7005
|
+
const res = await (0, import_light_my_request.default)(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
|
|
7011
7006
|
return new import_msw.HttpResponse(res.body, {
|
|
7012
7007
|
status: res.statusCode,
|
|
7013
7008
|
headers: mapHeaderType(res.headers)
|
|
7014
7009
|
});
|
|
7015
7010
|
}),
|
|
7011
|
+
import_msw.http.head(`${this.options.apiHost}/*`, async ({ request }) => {
|
|
7012
|
+
const body = await request.text();
|
|
7013
|
+
const url = new URL(request.url);
|
|
7014
|
+
const headers = copyHeaders(request.headers);
|
|
7015
|
+
const res = await (0, import_light_my_request.default)(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
|
|
7016
|
+
if (res.statusCode === 200) {
|
|
7017
|
+
const parsedBody = JSON.parse(res.body);
|
|
7018
|
+
const resultCount = "count" in parsedBody ? parsedBody.count : Object.keys(parsedBody).length;
|
|
7019
|
+
return new import_msw.HttpResponse(null, {
|
|
7020
|
+
status: resultCount > 0 ? 200 : 404,
|
|
7021
|
+
headers: mapHeaderType(res.headers)
|
|
7022
|
+
});
|
|
7023
|
+
}
|
|
7024
|
+
return new import_msw.HttpResponse(null, {
|
|
7025
|
+
status: res.statusCode,
|
|
7026
|
+
headers: mapHeaderType(res.headers)
|
|
7027
|
+
});
|
|
7028
|
+
}),
|
|
7016
7029
|
import_msw.http.get(`${this.options.apiHost}/*`, async ({ request }) => {
|
|
7017
7030
|
const body = await request.text();
|
|
7018
7031
|
const url = new URL(request.url);
|
|
7019
7032
|
const headers = copyHeaders(request.headers);
|
|
7020
|
-
const res = await (0, import_light_my_request.default)(
|
|
7033
|
+
const res = await (0, import_light_my_request.default)(app).get(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
|
|
7021
7034
|
return new import_msw.HttpResponse(res.body, {
|
|
7022
7035
|
status: res.statusCode,
|
|
7023
7036
|
headers: mapHeaderType(res.headers)
|
|
@@ -7027,7 +7040,7 @@ var CommercetoolsMock = class {
|
|
|
7027
7040
|
const body = await request.text();
|
|
7028
7041
|
const url = new URL(request.url);
|
|
7029
7042
|
const headers = copyHeaders(request.headers);
|
|
7030
|
-
const res = await (0, import_light_my_request.default)(
|
|
7043
|
+
const res = await (0, import_light_my_request.default)(app).post(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
|
|
7031
7044
|
return new import_msw.HttpResponse(res.body, {
|
|
7032
7045
|
status: res.statusCode,
|
|
7033
7046
|
headers: mapHeaderType(res.headers)
|
|
@@ -7037,14 +7050,26 @@ var CommercetoolsMock = class {
|
|
|
7037
7050
|
const body = await request.text();
|
|
7038
7051
|
const url = new URL(request.url);
|
|
7039
7052
|
const headers = copyHeaders(request.headers);
|
|
7040
|
-
const res = await (0, import_light_my_request.default)(
|
|
7053
|
+
const res = await (0, import_light_my_request.default)(app).delete(url.pathname + "?" + url.searchParams.toString()).body(body).headers(headers).end();
|
|
7041
7054
|
return new import_msw.HttpResponse(res.body, {
|
|
7042
7055
|
status: res.statusCode,
|
|
7043
7056
|
headers: mapHeaderType(res.headers)
|
|
7044
7057
|
});
|
|
7045
7058
|
})
|
|
7046
7059
|
);
|
|
7047
|
-
|
|
7060
|
+
}
|
|
7061
|
+
startServer() {
|
|
7062
|
+
if (_globalListeners.length > 0) {
|
|
7063
|
+
if (this._mswServer !== void 0) {
|
|
7064
|
+
throw new Error("Server already started");
|
|
7065
|
+
} else {
|
|
7066
|
+
console.warn("Server wasn't stopped properly, clearing");
|
|
7067
|
+
_globalListeners.forEach((listener) => listener.close());
|
|
7068
|
+
}
|
|
7069
|
+
}
|
|
7070
|
+
const server = (0, import_node.setupServer)();
|
|
7071
|
+
this.registerHandlers(server);
|
|
7072
|
+
server.listen({
|
|
7048
7073
|
// We need to allow requests done by supertest
|
|
7049
7074
|
onUnhandledRequest: (request, print) => {
|
|
7050
7075
|
const url = new URL(request.url);
|
|
@@ -7054,7 +7079,8 @@ var CommercetoolsMock = class {
|
|
|
7054
7079
|
print.error();
|
|
7055
7080
|
}
|
|
7056
7081
|
});
|
|
7057
|
-
_globalListeners.push(
|
|
7082
|
+
_globalListeners.push(server);
|
|
7083
|
+
this._mswServer = server;
|
|
7058
7084
|
}
|
|
7059
7085
|
};
|
|
7060
7086
|
// Annotate the CommonJS export names for ESM import in node:
|