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

Angular 20 and fetching files in unit tests

Discussão em 'Angular' iniciado por Chris Barr, Julho 26, 2025.

  1. Chris Barr

    Chris Barr Guest

    I have an Angular 19 app which I just upgraded to version 20 and now all of my unit tests which fetch files to process are failing with 404 errors. I have changed noting at all in my unit tests or test configuration.

    In my karma.config.js file I specify the file pattern I want Karma to be able to serve (karma docs):

    files: [
    {
    pattern: "test/sample-files/**/*.*",
    watched: false,
    served: true,
    included: true,
    },
    ],


    I made a unit test helper function for fetching file data:

    async function loadTestFileAsRawDataFile(folderPath: string, fileName: string): Promise<IRawDataFile> {
    const path = `base/test/sample-files/${folderPath}/${fileName}`;
    const response = await fetch(path);

    if (response.statusText !== 'OK') {
    throw new Error(
    `Test file at '${path}' could not be fetched! Is it included in the karma.conf.js files list?`,
    );
    }

    const fileNameParts = Utils.getFileNameParts(fileName);
    const dataAsBuffer = await response.arrayBuffer();

    return {
    name: fileNameParts.name,
    ext: fileNameParts.ext,
    type: response.type,
    dataAsBuffer,
    dataAsString: TestUtils.decoder.decode(dataAsBuffer),
    };
    }


    And then I would call that within my unit tests like this:

    fit('should properly ACCEPT a plain text file when tested', async () => {
    const testFile = await TestUtils.loadTestFileAsRawDataFile('Plain Text', 'empty.txt');
    expect(inputConverter.doesInputFileMatchThisType(testFile)).toBeTrue();
    });


    This is what my file structure looks like, which is unchanged with the upgrade to Angular 20

    [​IMG]

    And when I run my tests (just focusing one here), it's still requesting the URL I would expect, but it's just getting a 404 back now [​IMG]

    Why is this happening all of a sudden, and what do I need to change to fix this?

    Continue reading...

Compartilhe esta Página