jeudi 2 août 2018

Run Angular Tests Scripts From Docker

We have an angular application that uses jasmine and karma to run tests. As part of our tests, we get a nice UI browser popup that gives a lot of useful development information. While this is great for development, and I really want is some way of having a command that gives a result of success or failure for all tests instead.

The purpose of this is to build it into a Dockerfile so we have an easy pass/fail on tests. For example, one of our services does this:

FROM build AS test
WORKDIR /src
COPY Service.Project.Testing/Service.Project.Testing.csproj Service.Project.Testing/
RUN dotnet restore Service.Project.Testing/Service.Project.Testing.csproj
COPY . .
WORKDIR /src/Service.Project.Testing
RUN dotnet test

Which for dotnet will cause the docker compose to fail if any of our tests fail, which is desired. I could do the same thing with npm test instead of dotnet test, but I do not believe we will get a pass/fail on it, as the browser window is created regardless of tests passing or failing.

We have a test script that executes that I believe creates all of that (I was not the implementer, so I'm not certain all of its functionality) but I believe the piece that builds the UI is after the comment "First, initialize the Angular testing environment". As I mentioned, i do not want to lose this functionality for development, but I want a simple pass/fail for docker compose, similar to how dotnet test works.

// Prevent Karma from running prematurely
__karma__.loaded = function () { };

// First, initialize the Angular testing environment
testing.getTestBed().initTestEnvironment(
  testingBrowser.BrowserDynamicTestingModule,
  testingBrowser.platformBrowserDynamicTesting()
);

// Then we find all the tests
const context = require.context('../', true, /\.spec\.ts$/);

// And load the modules
context.keys().map(context);

// Finally, start Karma to run the tests
__karma__.start();

Has anyone done this testing before, and is there an easy way of doing it without breaking our nice UI for development? I imagine it has something to do with an npm run with some kind of similar script to the above, but without the ui components, I just have not been able to get that to function.

Aucun commentaire:

Enregistrer un commentaire