Having updated an Angular v19 app to Angular v20, the Google Maps API is not being loaded in the Karma unit tests. All the unit tests on components which serve Google Maps fail with the following message:
Error: Namespace google not found, cannot construct embedded google map. Please install the Google Maps JavaScript API: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API
In the test setup, I load the Google Maps API in the Karma configuration file (karma.config.js
) like this:
module.exports = function (config) {
config.set({
// ...
files: ["src/assets/google-maps-api.js"], <--
// ...
The files
property refers to a minified JavaScript file with Google Maps API.
The above setup continues to work in Angular v19, but not in Angular v20.
Does the Google Maps API need to be loaded differently in Angular v20 Karma unit tests?
Try adding the JS file, to the angular.json
, file's scripts
array, specifically for test cases.
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
],
"scripts": [
"/src/assets/google-maps-api.js",
],