1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Import a text file as a string in a Jest test in an Angular project

Discussão em 'Angular' iniciado por Bill Lynch, Setembro 13, 2024.

  1. Bill Lynch

    Bill Lynch Guest

    I'm trying to import a text file as a string to inject some data for testing.

    import rawStr from '../../testdata/testdata.txt';


    This fails:

    $ npm test

    > angular@0.0.0 test
    > ng test

    NOTE: The Jest builder is currently EXPERIMENTAL and not ready for production use.
    Application bundle generation failed. [2.806 seconds]

    ✘ [ERROR] TS2307: Cannot find module '../../testdata/testdata.txt' or its corresponding type declarations. [plugin angular-compiler]

    src/util/diff.component.spec.ts:6:22:
    6 │ import rawStr from '../../testdata/testdata.txt';
    ╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~


    How can I tell the Angular compiler (or maybe the Jest compiler?) that we should import the contents of the file as a string?

    angular.json:

    {
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
    "angular": {
    "projectType": "application",
    "root": "",
    "sourceRoot": "src",
    "architect": {
    "build": {
    "builder": "@angular-devkit/build-angular:application",
    "options": {
    "outputPath": "dist/angular",
    "index": "src/index.html",
    "browser": "src/main.ts",
    "polyfills": [],
    "tsConfig": "tsconfig.app.json",
    "assets": [
    "src/favicon.ico",
    "src/assets"
    ],
    "styles": [
    "src/styles.css",
    "node_modules/@fontsource/roboto/300.css",
    "node_modules/@fontsource/roboto/400.css",
    "node_modules/@fontsource/roboto/500.css",
    "node_modules/@fontsource/material-icons/400.css",
    "node_modules/bootstrap/dist/css/bootstrap.css",
    "node_modules/bootstrap-icons/font/bootstrap-icons.css"
    ],
    "scripts": [
    "node_modules/bootstrap/dist/js/bootstrap.js"
    ]
    },
    "configurations": {
    "production": {
    "outputHashing": "all"
    },
    "development": {
    "optimization": false,
    "extractLicenses": false,
    "sourceMap": true
    }
    },
    "defaultConfiguration": "production"
    },
    "serve": {
    "builder": "@angular-devkit/build-angular:dev-server",
    "configurations": {
    "production": {
    "buildTarget": "angular:build:production"
    },
    "development": {
    "buildTarget": "angular:build:development"
    }
    },
    "defaultConfiguration": "development"
    },
    "extract-i18n": {
    "builder": "@angular-devkit/build-angular:extract-i18n",
    "options": {
    "buildTarget": "angular:build"
    }
    },
    "test": {
    "builder": "@angular-devkit/build-angular:jest",
    "options": {
    "tsConfig": "tsconfig.spec.json"
    }
    }
    }
    }
    }
    }


    I'm using jest@29.7.0 and angular/cli@18.2.3.

    Continue reading...

Compartilhe esta Página