Installation
The package offers a schematics to install the package and add the needed file changes in your spartacus project.
Schematics installation
Run the angular schematics
ng add @valantic/spartacus-mockto install the package, add all needed changes and generate the boilerplate files.Check all the file changes made by the schematics, restore potential previous custom changes, as the adjusted files where copied from spartacus-mock
Optional: If you use PWA functionality, enhance your
ngsw-config.jsonsectionappconfigfilesarray with"!/*mock*.js". This makes sure, the service worker does not load the mock server files.{ "name": "app", "installMode": "prefetch", "resources": { "files": [ "/favicon.ico", "/index.html", "/manifest.webmanifest", "/*.css", "/*.js", "!/*mock*.js" ] } }Optional: if you use prettier, add the file
src/mockServiceWorker.jsto the.prettierignorefile
Manual installation
Open a terminal and navigate to the root folder of your angular / spartacus project (the folder where the
angular.jsonis located)Run
npm i @valantic/spartacus-mock@3.1.0 -dto install the package as devDependencyRun
npm i msw@2.2.3 -dto install the package as devDependencyEnhance
angular.jsonarchitect build target assets array with"src/mockServiceWorker.js"Enhanced
package.jsonwith the following object in the root level"msw": { "workerDirectory": "src" }Enhance your
src/environments/environment.tsfile with the following properties (these properties are needed for the mock server to work)mockServer: true,with the following object in the root level
backend: { occ: { baseUrl: '<your-spartacus-dev-env-url>', prefix: '/occ/v2/', }, },Add an
src/environments/environment.model.tsto have better type safetyReplace the content of the file
src/main.tswith the following content:import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; if (environment.production) { enableProdMode(); } async function prepare(): Promise< ServiceWorkerRegistration | undefined > { if (!environment.mockServer) { return undefined; } const { prepareMockServer } = await import( /* webpackChunkName: "mock-server" */ './mock-server' ); return prepareMockServer(); } function bootstrap() { platformBrowserDynamic() .bootstrapModule(AppModule) .catch((err) => console.error(err)); } if (document.readyState === 'complete') { prepare().then(() => bootstrap()); } else { document.addEventListener('DOMContentLoaded', () => prepare().then(() => bootstrap()) ); }Merge potential previous custom changes with the above content, if you have any
Optional: Run the schematics
ng boilerplate @valantic/spartacus-mockand check the boilerplate files generated in thesrc/mock-serverfolder for an easier startsrc/mock-server/index.tssrc/mock-server/routes.tssrc/mock-server/handlers.tssrc/mock-server/pass-through.tssrc/mock-server/mock-data/languages/languages.ts
Optional: If you use PWA functionality, enhance your
ngsw-config.jsonappconfigfilesarray with"!/*mock*.js". This makes sure, the service worker does not load the mock server files.
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/favicon.ico",
"/index.html",
"/manifest.webmanifest",
"/*.css",
"/*.js",
"!/*mock*.js"
]
}
}Last updated