Property 'only' does not exist on describe - mocha.js

I'm trying to test an application with Mocha.
In Mocha I should be able to use the only feature to select a sublist of tests to run, like:
describe.only('my tests', () => {
// my tests...
})
instead of the usual:
describe('my tests', () => {
// my tests...
})
But only returns the compile-time error:
Property 'only' does not exist on type ...
describe and it are well recognized and work fine.
Why is only not recognized? It is part of the official Mocha, is not an extension or similar.

I don't know the cause, but adding this at the beginning solved the problem:
import { it, describe } from 'mocha'
Now I'm able to compile the TypeScript code and the tests are executed fine.
Note: don't include only in the imports.

Related

Is the code actually executed when using a Mockery spy?

I am trying to write some tests with a Mockery Spy. However, it doesn't seem like the "spied upon" code is executed when I'm using a spy.
Is the code actually executed when using a Mockery spy?
Here is what I tested:
// In my test:
$spy = $this->spy(FeedManager::class);
// Controller
resolve(FeedManager::class)->createResponse();
// FeedManger::createResponse()
public static function createResponse(Builder $builder)
{
dd("here i am"); // this never gets called unless I remove the spy
}
Apparently, the "spy code" is not supposed to run. Found this quote from the docs.
The \Mockery::spy() method call is actually a shorthand for calling
\Mockery::mock()->shouldIgnoreMissing(). The shouldIgnoreMissing
method is a “behaviour modifier”.

Mocking out a function inside another function

I don't actually want 'anotherFunction' to execute.
But rather when it's called inside someFunction, to have it return a specific value rather than actually executing:
// Testing this function
export function someFunction(foo, bar): string {
// want to provide a mock result here
const baz = anotherFunction(foo, bar);
// do something unrelated
}
export function anotherFunction(quz, quux): any {
// do something unrelated
}
How would you go about this with jasmine? The examples I find all assume a class and then use:
// Can't use this as the method I'd like to "mock out" is not in a class
const spy = spyOn(someClass, 'aMethod');
I'm looking for something similar to the mock function in Jest. That documentation helps communicate my question better:
"Mock functions allow you to test the links between code by erasing the actual implementation of a function, ..."
But then something similar in Jasmine.
Try something like this:
import * as helpers from './file/where/anotherFunction/is';
...
spyOn(helpers, 'anotherFunction');
Check this link out.

Mocha + Chai + Sinon stub- TypeError: Cannot read property 'set' of undefined

In nodejs I am in an express controller that has the response object doing this as the second statement:
res.set('Content-Type', 'application/json');
When I try to stub it out
sinon.stub(contractorController.putcontractor, 'set').resolves(true);
I get the error
TypeError: Cannot stub non-existent own property
I've tried many things, please help. This controller is being imported with a require statement and the controller itself is a series of exported functions (No classes). I have been able to get this to work when I create classes, but I am not certain I am supposed to be refactoring all this code into classes just so the unit tests will work.
In express while unit testing the req and res objects with all their related methods both need to be stubbed.
const res = {
set: sinon.stub(),
get: sinon.stub(),
...
}

Cypress Fixture: getting ReferenceError when trying to use aliases

I am new to Cypress, I am trying to use fixtures in my code, and keep getting ReferenceError if I used it aliasname.variablename fashion.
describe('Fixture Demo', function() {
it('This works', function() {
cy.fixture('admindetails').then((user) => {
cy.log(user.username)
})
})
it('This does not work', function() {
cy.fixture('admindetails').as(user)
cy.log(user.username)
})
})
I included both what is working and what is not. My question is how can I simple use user.username using alias? I even tried #user.username and it doesn't work. I am trying to make 2nd test work. Any help would be highly appreciated. Thanks in advance.
Your second test case isn't a valid way to do this, your first test is the correct way.
user is just a normal Javascript variable. In your second test, you are trying to use the user variable before it has been defined, which is why you get the error.
Here is the correct way to use aliases, from the docs:
cy.fixture('admindetails').as('usersJSON')
cy.route('GET', '/users/**', '#usersJSON') // or another command that supports aliasing
Your first test case is also fine to use as well, if you need the fixture contents later on they will be stored as user:
cy.fixture('admindetails').then((user) => {
cy.log(user.username)
/** add any other test code that needs `user` here **/
})
/** write the rest of your tests here, they will execute after the .then block **/

Laravel integration test throwing spurious 404 error when run as a class, individual tests work fine

I'm using the new integration testing stuff in L5.1. I'm getting some strange errors when running a test class - mostly 404's on the pages I'm testing. Oddly, when I filter down to an individual test the test passes fine. When I run the whole test class or test suite that it's a part of, it fails with a 404. The route works in the browser, and the test passes when I run by itself, so it's clearly not a valid error.
The code looks something like this:
class MyTest extends \TestCase
{
use WithoutMiddleware;
public function __construct() {
parent::__construct();
$this->mock = m::mock('MyApp\Stuff\Repository');
$this->validator = m::mock('Illuminate\Validation\Factory');
$this->mockedClass = m::mock('MyApp\Stuff\Service');
}
/**
* #test
*/
public function it_should_get_all_thingies() {
$this->mockedClass->shouldReceive('someMethod')
->once()
->andReturn('yay');
$this->app->instance('MyApp\Stuff\Service', $this->mockedClass);
$this->visit('/api/v1/thingies');
}
}
When I run
phpunit --filter=it_should_get_all_thingies , it works fine.
When I run
phpunit --filter=MyTest, it dies with a 404. I can copy the relevant URL from the error message into the browser and it works fine.
The only other relevant fact that I can think of is that this on an updade from L4.2 to 5.0 to 5.1.
Any help is greatly appreciated.
Figured out what was triggering the error and how to work around it. I was pulling in a secondary routes file like this:
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'api/v1'], function() {
require_once_app_path(__DIR__ . '/routes2.php');
});
The require_once_app_path triggers the issue. Didn't happen in L4.2 or L5.0, started when we upgraded to 5.1. Replacing that with require seems to sort things out.

Resources