run()
will create a component of this type before running the rest of your test.
passed along to TestBed.configureTestingModule()
. Automatically includes NoopAnimationsModule, in addition to those provided by AngularContext.
By default a synthetic parent component will be created that binds to all your component's inputs. Pass input names here that should NOT be bound. This is useful e.g. to test the default value of an input.
The ComponentFixture for a synthetic wrapper around your component. Available with the callback to run()
.
Set this before calling run()
to mock the time at which the test starts.
Assign inputs passed into your component. Can be called before run()
to set the initial inputs, or within run()
to update them and trigger all the appropriate change detection and lifecycle hooks.
Assign css styles to the div wrapping your component. Can be called before or during run()
. Accepts an object with the same structure as the ngStyle directive.
ctx.assignWrapperStyles({
width: '400px',
height: '600px',
margin: '20px auto',
border: '1px solid',
});
Protected
cleanPerforms any cleanup needed at the end of each test. This implementation destroys fixture and calls the super implementation.
Protected
initGets a service or other injectable from the root injector.
This implementation is a simple pass-through to TestBed.inject()
, but subclasses may provide their own implementation. It is recommended to use this in your tests instead of using TestBed
directly.
Runs test
in a fakeAsync
zone. It can use async/await, but be sure anything you await
is already due to execute (e.g. if a timeout is due in 3 seconds, call .tick(3000)
before await
ing its result).
Also runs the following in this order, all within the same zone:
this.init()
test()
this.verifyPostTestConditions()
this.cleanUp()
Protected
runAdvance time and trigger change detection. It is common to call this with no arguments to trigger change detection without advancing time.
The unit of time amount
represents. Accepts anything described in @s-libs/s-core
's [TimeUnit]https://simontonsoftware.github.io/s-js-utils/typedoc/enums/timeunit.html
enum.
Protected
verifyRuns post-test verifications. This base implementation runs HttpTestingController.verify
and MockErrorHandler.verify. Unlike #cleanUp, it is OK for this method to throw an error to indicate a violation.
Static
getReturns the current AngularContext
that is in use, or undefined
if there is not one. A context is defined to be "in use" from the time it is constructed until after its run()
method completes.
Provides the foundation for an opinionated pattern for component tests.
run()
.ngOnChanges()
like it would in production. This is not the case if you use the standardTestBed.createComponent()
directly.fakeAsync
zone, which is normally a challenge.tick()
fakeAsync
zone, i.e. with the callback to #run. If you have async initializers, you must be careful not to do things that finalize the app setup before then, such as #inject.A very simple example:
A full example, with routing and a component harness. This is the full code for a tiny Angular app: