Getting ready

Let's explore how to add some API integration tests to our Express web server's REST API. In order to work with our API, we'll need to install a Chai plugin called chai-http, which will work as a lightweight request library to make HTTP request assertions in our tests:

npm install chai-http @types/chai-http --save-dev

Another detail we need to address is that the /src/routes/Angular.ts module of our application relies on our Angular source files being inside the /dist directory. We can circumvent this by simply making this route optionally provided via an environment variable:

...
const angularBuildPath = process.env.angularBuildPath ? process.env.angularBuildPath : path.resolve(__dirname, 'angular');

class AngularRoute {
public route: Router;
private angularBuildPath: string;
private indexPage: CheerioStatic;

constructor() {
this.route = express.Router();
this.angularBuildPath = String(angularBuildPath);
this.indexPage = this.getIndexPage(this.angularBuildPath);
this.enableGzipCompression();
this.route.get('*', this.serveRoute.bind(this));
}
...
}

export default new AngularRoute().route;

This way, all we need to do is export this environment variable the same way we do for any of our environment variables, such as our cookie secret or API keys:

export angularBuildPath /Users/nicholas/my-projects/my-express-project/dist/angular

This will now allow us to run our project fully, without relying on the source built into the /dist directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.242.118