billtrik / karma-fixture

A plugin for the Karma test runner that loads .html and .json fixtures
MIT License
77 stars 10 forks source link

Cannot find fixture error, unknown what to do to counter act this. #20

Open ghost opened 7 years ago

ghost commented 7 years ago

Hi,

I'm trying to implement the karma fixture plugin for the first time into my angular 1 project.

Versions from package.json that are relevant:

When I follow the guide and implement everything I'm getting:

ReferenceError: Cannot find fixture '{projectpath}/test/fixtures/base/path/test1.json' in http://localhost:9876/__karma__{projectpath}/node_modules/karma-fixture/lib/fixture.js (line 141)

My folder structure for my test framework is

test - fixtures - base - path - json - test1.json

Which I followed the guide just to implement but as I said I'm getting the error above when I run the tests. Obviously the path I'm running is correct from following my directory structure. Any help would be much appreciated.

Thanks,

Garry

billtrik commented 7 years ago

Please check out https://github.com/billtrik/karma-fixture/issues/15 especially the part with adding debugger before fixture.load() and checking out the contents of window.__json__.

ghost commented 7 years ago

Hi @billtrik thanks very much for your reply.

I added the stuff in as you said, not sure 15 was the correct issue but I just added debugger than debugged the code.

Hopefully you can help me out, many thanks again for your assistance.

So this is my test json which is situated in: screen shot 2017-01-26 at 14 57 58

screen shot 2017-01-26 at 14 56 18

screen shot 2017-01-26 at 14 53 39

and got this

screen shot 2017-01-26 at 14 54 03

Yet I still get the issue I shown here: screen shot 2017-01-26 at 14

billtrik commented 7 years ago

Have you already done a fixture.setBase()? Could you please tell me the value of fixture.base?

jlison commented 7 years ago

Hi billtrik,

I am having the same problem as girwin575. I tried to debug the code a little bit, and I found out that in: karma-fixture/lib/fixture.js the object __html__ is empty when your test folder is not in the root dir of the project. For example the following example does NOT work for me:

my_projects
     |__ widget
             |__ test
                     |__ spec
                     |       |__ExampleSpec.js
                     |
                     |__ html
                             |__myhtml.fixture.html

However, karma-fixture works for me with this folder structure:

my_projects
        |__ test
                 |__ spec
                 |       |__ExampleSpec.js
                 |
                 |__ html
                         |__myhtml.fixture.html

In my ExampleSpec.js file I have:

...
  // Inject the HTML fixture for the tests
  beforeEach(() => {
    fixture.base = 'widget/test';
    fixture.load('html/myhtml.fixture.html');
  });
...

|=> Which does NOT work with the first folder structure

  // Inject the HTML fixture for the tests
  beforeEach(() => {
    fixture.base = 'test';
    fixture.load('html/myhtml.fixture.html');
  });

|=> But in this case, it DOEST work with the second folder structure (test folder on the project's root dir)

I also tried for the first folder structure :

  // Inject the HTML fixture for the tests
  beforeEach(() => {
   fixture.setBase('widget/test')
    fixture.load('html/myhtml.fixture.html');
  });

and

  // Inject the HTML fixture for the tests
  beforeEach(() => {
    fixture.setBase('./widget/test')
    fixture.load('html/myhtml.fixture.html');
  });

In each of the last two cases, where the fixture html could not be found, the value of fixture.base was always as expected (I console.log it out in Fixture.prototype.load in karma-fixture/lib/fixture.js), but the value of the object __html__ was empty. On the other hand, in the second folder structure case (the one that worked, with thetest folder in root dir), the object __html__ contained the html code to be injected in the test (*.spec) file.

I hope my comment can be somehow helpful.

Thank you for your time and for creating and maintaining the karma-fixture module!

jlison commented 7 years ago

Ok, I realized that I wasn't using the right pattern to inject the HTML files in my karma.conf.js.

Once I changed:

files: [
      '. widget/test/*.html'
    ]

for:

files: [
      './widget/test/**/*.html'
    ]

Everything worked as expected.

Have a good one folks, and please pardon my stupidity!

ghost commented 7 years ago

@billtrik

fixture.setBase('base/test/unit/fixtures/json')

ghost commented 7 years ago

@billtrik is there anything I can do to help with progress on this?

billtrik commented 7 years ago

@girwin575 is it possible for you to share a public repo where i could debug the problem myself?

ghost commented 7 years ago

Thanks for the reply @billtrik I'll ask and find out.

ghost commented 7 years ago

@billtrik I can't share the repo but I'm going to clone the repo, strip out the client related stuff and get the same issues we've had before then tag you if that's ok?

billtrik commented 7 years ago

That would be great. Please go ahead and let me know

AneelaBrister commented 7 years ago

I am seeing this issue as well. Fixtures are not available at runtime.

Symptoms:

Json files that live in a subfolder are not loaded as fixtures at runtime.

Earlier:

I had mock data jsons for unit tests in a folder called mock-data directly under the main folder (where karma.conf.js lives). A) This was my 'files' value in karma.conf.js: files: [ { pattern: './src/test.ts', watched: false }, { pattern: 'mock-data/**/*.json' } ], B) And in the beforeEach, I set base like this: fixture.setBase('mock-data'); Everything worked fine.

Now:

After I upgraded to the latest Angular: ( @angular cli: 1.1.0, @angular/... 4.1.3), ng serve stopped serving up files from alternate locations, so I was forced to move the mock-data folder to under src\assets. A) Now my karma.conf.js files looks like this: files: [ { pattern: './src/test.ts', watched: false }, { pattern: 'src/assets/mock-data/**/*.json' } ], B) And I use this for setBase: fixture.setBase('src/assets/mock-data');

Now, it can no longer find the fixtures. I get an error: Uncaught ReferenceError: Cannot find fixture 'src/assets/mock-data/widget-grid-fake.json' at: Fixture._throwNoFixture at: Fixture.load ...

If I look at debug.html under Sources though, I see all my files loaded as .js in the window.karma variable: window.__karma__.files = { ... '/base/src/assets/mock-data/widget-grid-fake.js': 'f79fdbc965473b60492064577fa8e2a65ee0273b',

Any help will be appreciated! Thanks Aneela

p0lar-bear commented 7 years ago

I'm getting this issue as well, and I've taken the steps to check the json object contents as described above. Running my tests in IE 11.0.9600.18738CO, karma@1.7.0, karma-json-fixtures-preprocessor@0.0.6, and your plugin at 0.2.6.

For my config, I'm passing the glob ./mysite/js/myAngularModule/**/*.fixtures.json to Karma to add my fixtures, among the others for specs and such.

The glob works, because the fixture mysite/js/myAngularModule/core/component/my-element.fixtures.json will load fine if I put this into my Jasmine beforeAll() method:

fixture.setBase('mysite/js/myAngularModule/core/component');
this.fixtures = fixture.load('my-element.fixtures.json');

It will fail with a ReferenceError ("Cannot find fixture") if I change it to:

this.fixtures = fixture.load('/mysite/js/myAngularModule/core/component/my-element.fixtures.json');

At runtime, my fixture exists in window.__json__ like so:

{
    'mysite/js/myAngularModule/core/component/my-element.fixtures': [object]
}

Your checks on line 50-53 of fixture.coffee for json fixtures in window.__json__ use strings that're frustratingly close, but no cigar:

> filename.replace('.json', '')
"/mysite/js/myAngularModule/core/component/my-element.fixtures"
> filename
"/mysite/js/myAngularModule/core/component/my-element.fixtures.json"
> fixture_path
"mysite/js/myAngularModule/core/component/my-element.fixtures.json"
> this.base + "/" + (filename.replace('.json', ''))
"spec/fixtures//mysite/myAngularModule/core/component/my-element.fixtures"

Seems while you remembered to strip the leading slash from the fixture_path variable, you forgot to strip the .json suffix from it while doing those checks.

So the workaround here if you're not using the default base path is to set the base explicitly before each call to Fixture.load() that will load a file in a new directory.

robcornelius commented 6 years ago

I cannot get this to work at all.

I have followed all the steps in the setup correctly. No matter what I do I get 'ReferenceError: Cannot find fixture 'spec/fixtures/test1.html' I have tried using fixture.setBase() to change the base for my fixtures and I have put my fixtures in the 'default' location and nothing works.

My current set up is

karma.conf.js
   |__ spec
         |__fixtures
             |__test1.html

my karma.conf.js contains the following

frameworks: ['requirejs', 'qunit', 'sinon', 'fixture'],
files: [
    {pattern: 'spec/fixtures/**/*', included: false}
],
preprocessors: {
        'fixtures/**/*.html'   : ['html2js'],
        'fixtures/**/*.json'   : ['json_fixtures']
],
jsonFixturesPreprocessor: {
      variableName: '__json__'
},

by the way I am on windows10

If I try to add a plugins section to my karma.conf.js I get errors about requirejs frameworks going missing and nothing works at all.

There is something very wrong with how the code handles paths.

andreborud commented 6 years ago

I have the sam problem where the fixture can not be found. Using Karma 3.0.0 and 0.2.6 of this plugin.

I added a log message to ficture.js in the load() function. Like this:

Fixture.prototype.load = function() {
    ...
    __json__ = window.__json__ || {};
    for (var attachedDocs in __json__) {
        console.log(attachedDocs)
    }
    ...
}

This is part of the console output. The error below is the last of many. But what can been interesting is the three first lines which seems to be the only fixtures which has been loaded so far. Then after the last error the entire list of all my fixtures are there.

LOG: 'test/resources/21bbbb20-1bab-0132-514f-10b11c4ed8fd'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce_ToT'
HeadlessChrome 0.0.0 (Mac OS X 10.14.0) ParameterSet load route sports from file FAILED
        ReferenceError: Cannot find fixture 'test/resources/9e6fd650-338d-0133-9375-10bf48d758ce.json'
            at <Jasmine>
            at Fixture._throwNoFixture (node_modules/karma-fixture/lib/fixture.js:147:10)
            at Fixture.load (node_modules/karma-fixture/lib/fixture.js:71:12)
            at UserContext.<anonymous> (test/model/util/ParameterSetSpec.js:98:46)
            at <Jasmine>
LOG: 'test/resources/21bbbb20-1bab-0132-514f-10b11c4ed8fd'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce_ToT'
LOG: 'test/resources/21bbbb20-1bab-0132-514f-10b11c4ed8fd'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce_ToT'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce_gpsdata'
LOG: 'test/resources/7dc3fae0-fc7b-0132-9f7a-10bf48d758ce_markpassings'
LOG: 'test/resources/9e6fd650-338d-0133-9375-10bf48d758ce-gpsdata'
LOG: 'test/resources/9e6fd650-338d-0133-9375-10bf48d758ce-markpassings'
LOG: 'test/resources/9e6fd650-338d-0133-9375-10bf48d758ce'
LOG: 'test/resources/ESS2015-Cardiff-Race20-gpsdata'
LOG: 'test/resources/ESS2015-Cardiff-Race20-markpassings'
LOG: 'test/resources/ESS2015-Cardiff-Race20'
LOG: 'test/resources/bergen_city_marathon'
LOG: 'test/resources/d5590280-093e-0134-e3da-60a44ce903c3'
LOG: 'test/resources/e212ed90-2f17-0134-c478-60a44ce903c3'
LOG: 'test/resources/leaderboard-columns-hidden'
LOG: 'test/resources/leaderboard-columns-legacy'
LOG: 'test/resources/leaderboard-columns-visible'
LOG: 'test/resources/leaderboard-default-sorting-1'
LOG: 'test/resources/leaderboard-default-sorting-2'
LOG: 'test/resources/wind'

My guess is that the jsonFixturesPreprocessor didn't finish loading all the .json files before the tests start. Then somewhere in the middle all tests are loaded and the errors stop.

This is part of my karma config:

frameworks: ['requirejs', 'jasmine', 'fixture'],

        plugins: [
            'karma-chrome-launcher',
            'karma-fixture',
            'karma-html2js-preprocessor'
        ],

        // Include the test and source files
        files: [
            {
                pattern: 'test/resources/**/*.json',
                included: true
            }
        ],

        preprocessors: {
            'test/resources/**/*.html'   : ['html2js'],
            'test/resources/**/*.json'   : ['json_fixtures']
        },

        jsonFixturesPreprocessor: {
            variableName: '__json__'
        }

Could this be the cause of the problems?

elalemanyo commented 5 years ago

Hi, I also had some problems finding fixtures, the problem in my case was that the karma.conf.js file was not at my root directory. To be able to keep the config file there and also find the fixtures I needed to set the basePath at the karma.conf.js:

module.exports = function(config) {
  config.set({
    basePath: '../../../',

In my case the file is three directories under the project root.

Maybe helps someone.